# Python sets

• A set is an unordered collection of unique and unhashable objects.
• So, tuples, lists and dictionaries, etc. can be members of a set.
• `set` is mutable so, can add/remove elements to/from the set.
• `set` can't allow elements to be replaced

## create a set¶

``````unique_numbers = {1,2,3,4}
names = set(["Anji", "Smita"])
``````

## set - no duplicate elements¶

``````duplicates = [1,2,3,4,5,5,4,3,2,6]
unique = set(duplicates)
print(unique)
# Output: {1, 2, 3, 4, 5, 6}
``````

• use method `set.add()` to add element to set.
``````names = {"John", "Jenny", "Lilly"}
print(names)
# Output: {'Anji', 'Lilly', 'Jenny', 'John'}
``````

## remove element from set¶

• use method `set.remove()` to remove element from set.
• it raises an error if element doesn't exist.
``````names = {"John", "Jenny", "Lilly"}
names.remove("Jenny")
print(names)
# Output: {'Lilly', 'John'}
``````
• use method `set.discard()` to remove element from set
• unlike `set.remove()` it didn't throw an error.
``````names = {"John", "Jenny", "Lilly"}
print(names)
# Output: {'Lilly', 'John'}
``````

## set - `for` loop¶

``````fruits = {"apple", "banana", "cherry"}
for fruit in fruits:
print(fruit)
# Output:
# cherry
# apple
# banana
``````

## check if element exist in set¶

• use `in` operator to check if element exist
``````fruits = {"apple", "banana", "cherry"}
print("apple" in fruits)
# Output: True
``````

## check if element doesn't exist in set¶

• use `not in` operator to check if element does not exist
``````fruits = {"apple", "banana", "cherry"}
print("apple" not in fruits)
# Output: True
``````

## update set with any iterable¶

• use method `set.update()` to update set with any iterable like `list`, `tuple`, `set`, etc.
``````s1 = {1,2,3}
s2 = {4,5,6}

s1.update(s2)
print(s1)
# Output: {1, 2, 3, 4, 5, 6}

s1.update([7,8,9])
print(s1)
# Output: {1, 2, 3, 4, 5, 6, 7, 8, 9}
``````

## remove random element from set¶

• use `set.pop()` to remove random element
``````numbers = {1,2,3,4,5,6}
numbers.pop()
print(numbers)
# Output: {2, 3, 4, 5, 6}
``````

## find set difference¶

• use `set.difference()` method or `-` operator.
``````mammals = {"Lion", "Deer", "Bat"}
fliers = {"Parrot", "Eagle", "Bat"}

diff = mammals - fliers
print(diff)
# Output: {'Lion', 'Deer'}

print(mammals.difference(fliers))
# Output: {'Lion', 'Deer'}
``````

## find set symmetric difference¶

• it can be done in two ways
• can use `^` operator or method `set1.symmetric_difference(set2)`
``````mammals = {"Lion", "Deer", "Bat"}
fliers = {"Parrot", "Eagle", "Bat"}

diff = mammals ^ fliers
print(diff)
# Output: {'Deer', 'Eagle', 'Lion', 'Parrot'}

print(mammals.symmetric_difference(fliers))
# Output: {'Deer', 'Eagle', 'Lion', 'Parrot'}
``````

## find union of sets¶

• use `|` operator or method `set1.union(iterable)`
• iterable can be a `list`, `tuple`, `set`, etc.
``````mammals = {"Lion", "Deer", "Bat"}
fliers = {"Parrot", "Eagle", "Bat"}

union = mammals | fliers
print(union)
# Output: {'Lion', 'Deer', 'Bat', 'Eagle', 'Parrot'}

print(mammals.union(fliers))
# Output: {'Lion', 'Deer', 'Bat', 'Eagle', 'Parrot'}
``````

## find intersection of sets¶

• use `&` operator or method `set1.intersection(iterable)`
• iterable can be a `list`, `tuple`, `set`, etc.
``````mammals = {"Lion", "Deer", "Bat"}
fliers = {"Parrot", "Eagle", "Bat"}

common = mammals & fliers
print(common)
# Output: {'Bat'}

print(mammals.intersection(fliers))
# Output: {'Bat'}
``````

## check if sets are disjoint¶

• use method `set.isdisjoint(iterable)`
• iterable can be a `list`, `tuple`, `set`, etc
``````mammals = {"Lion", "Deer"}
fliers = {"Parrot", "Eagle", "Bat"}

is_disjoint = mammals.isdisjoint(fliers)
print(is_disjoint)
# Output: True
``````

## check if set is subset of other set¶

• use method `set.issubset(iterable)`
• iterable can be a `list`, `tuple`, `set`, etc.
``````s1 = {1,2,3,4,5,6,7,8}
s2 = [1,2]

print(s2.issubset(s1))
# Output: True
``````

## check if set is superset of other set¶

• use method `set.issuperset(iterable)`
``````s1 = {1,2,3,4,5,6,7,8}
s2 = (1,2)

print(s1.issuperset(s2))
# Output: True
``````

## using operators `in` and `not in` with sets¶

``````s = {1,2,3,4}
print("a" in s)
# Output: False
print("a" not in s)
# Output: True
``````

## set methods¶

Method Description
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() Inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others