Sets
Sets¶
- Sets are used to store multiple items in a single variable.
- Sets are unordered, unchangeable (but you can add/remove items), and do not allow duplicate values.
In [1]:
Copied!
## Creating a Set
fruits_set = {'apple', 'banana', 'cherry'}
print(fruits_set)
## Creating a Set
fruits_set = {'apple', 'banana', 'cherry'}
print(fruits_set)
{'cherry', 'banana', 'apple'}
Note : Since Set is Unordered, there is no indexing and slicing¶
No Duplicates Allowed¶
In [3]:
Copied!
## Duplicate values will be removed automatically
fruits_set = {'apple', 'banana', 'apple'}
print(fruits_set)
## Duplicate values will be removed automatically
fruits_set = {'apple', 'banana', 'apple'}
print(fruits_set)
{'banana', 'apple'}
Add Items¶
In [4]:
Copied!
fruits_set = {'apple', 'banana'}
fruits_set.add('cherry')
print(fruits_set)
fruits_set = {'apple', 'banana'}
fruits_set.add('cherry')
print(fruits_set)
{'cherry', 'banana', 'apple'}
Add Multiple Items¶
In [7]:
Copied!
fruits_set.update(['orange', 'grape'])
print(fruits_set)
fruits_set.update(['orange', 'grape'])
print(fruits_set)
{'orange', 'apple', 'cherry', 'banana', 'grape'}
Remove Items¶
In [8]:
Copied!
fruits_set.remove('banana')
print(fruits_set)
fruits_set.remove('banana')
print(fruits_set)
{'orange', 'apple', 'cherry', 'grape'}
Discard vs Remove¶
In [9]:
Copied!
fruits_set.discard('pineapple') # No error if not found
# fruits_set.remove('pineapple') would raise an error
fruits_set.discard('pineapple') # No error if not found
# fruits_set.remove('pineapple') would raise an error
Set Operations¶
Set Methods in Python¶
Method | Description | Example |
---|---|---|
add(elem) |
Adds a single element to the set | s.add(5) |
update(iterable) |
Adds multiple elements from an iterable (like list, tuple) | s.update([1, 2]) |
remove(elem) |
Removes specified element; raises error if not found | s.remove(3) |
discard(elem) |
Removes specified element; no error if element not found | s.discard(10) |
pop() |
Removes and returns a random element | s.pop() |
clear() |
Removes all elements from the set | s.clear() |
copy() |
Returns a shallow copy of the set | s2 = s.copy() |
union(set2) |
Returns a new set with all elements from both sets | s.union(s2) |
intersection(set2) |
Returns common elements of both sets | s.intersection(s2) |
difference(set2) |
Returns elements in the first set but not in the second | s.difference(s2) |
symmetric_difference(set2) |
Returns elements in either set, but not in both | s.symmetric_difference(s2) |
issubset(set2) |
Returns True if all elements of the set are in set2 |
s.issubset(s2) |
issuperset(set2) |
Returns True if set has all elements of set2 |
s.issuperset(s2) |
isdisjoint(set2) |
Returns True if sets have no elements in common |
s.isdisjoint(s2) |
In [ ]:
Copied!
# Union
set1 = {'a', 'b'}
set2 = {'b', 'c'}
print(set1.union(set2))
# Union
set1 = {'a', 'b'}
set2 = {'b', 'c'}
print(set1.union(set2))
In [ ]:
Copied!
# Intersection
print(set1.intersection(set2))
# Intersection
print(set1.intersection(set2))
In [ ]:
Copied!
# Difference
print(set1.difference(set2))
# Difference
print(set1.difference(set2))
In [ ]:
Copied!
# Symmetric Difference
print(set1.symmetric_difference(set2))
# Symmetric Difference
print(set1.symmetric_difference(set2))
Looping Through a Set¶
In [10]:
Copied!
for fruit in fruits_set:
print(fruit)
for fruit in fruits_set:
print(fruit)
orange apple cherry grape
Check if Item Exists¶
In [11]:
Copied!
'apple' in fruits_set
'apple' in fruits_set
Out[11]:
True
In [ ]:
Copied!