Sets are used to store multiple items in a single variable.
A Set is a collection which is Unordered , unchangeable, and does not allow duplicate (unindexed) items. Set items can appear in a different order every time we use them, and cannot be referred to by index or key.
Sets are written with curly brackets .
my_Set = {'iphone','samsung','lg'}
print(my_Set)
create an empty Set:
my_Set= {}
len(my_Set) => 0
The set() Constructor :
my_Set =set(('iphone','samsung','lg')) # Note a double rounded brackets
print(my_Set)
Sets Properties :
- Unordered :
Unordered means that the items in a set do not have a defined order. Set items can appear in a different order every time we use them, and cannot be referred to by index or key.
- Unchangeable
Set items are not changeable, meaning that we cannot change the items after the set has been created.
- Duplicate Not Allowed
Sets cannot have two items with the same value.
Set Length
len() function is use to determine the length of a Set.
my_Set = {'iphone','samsung','lg'}
print(len(my_Set))
Access Set Items:
We cannot access items in a set by referring to an index or a key. To access an item we can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.
for x in my_Set: # Loop through the set, and print the values.
print(x)
print('samsung' in my_Set) # check if 'samsung' is present in the set.
Change Set Items
Once a Set is created, you cannot change its items. But we can add new items.
Add Items to Set
- To add one item to a set use the add() method.
my_Set = {'iphone', 'samsung', 'lg'} my_Set.add('Motorola')
- To add items from another set into the current set, use the update() method.
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'MI', 'Poco', 'Motorola'} my_Set1.update(my_Set2) print(my_Set1)
Remove Items
To remove an item in a set, we can use the remove(), or the discard() method.
Using remove() method. If the item to remove does not exist, remove() will raise an error.
my_Set = {'iphone', 'samsung', 'lg'} my_Set.remove("iphone")
Using discard() method.
my_Set = {'iphone', 'samsung', 'lg'} my_Set.discard("iphone")
Using pop() method. It will remove the last item. Remember that sets are unordered, so we will not know what item that gets removed. The return value of the pop() method is the removed item.
my_Set = {'iphone', 'samsung', 'lg'} x = my_Set.pop()
Using clear() method. It will empties the set.
my_Set = {'iphone', 'samsung', 'lg'} my_Set.clear()
Using del keyword. del keyword will delete the set completely.
my_Set = {'iphone', 'samsung', 'lg'} del my_Set
Loop Through a Set
using for loop:
for x in my_Set:
print(x)
Join Sets
- using union() method.
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'MI', 'Poco', 'Motorola'} my_Set3 = my_Set1.union(my_Set2) print(my_Set3)
- using intersection_update() method. It will keep only the items that are present in both sets.
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'iphone', 'Poco', 'Motorola'} my_Set1 = my_Set1.intersection_update(my_Set2) print(my_Set1)
- using intersection() method. It will return a new set, that only contains the items that are present in both sets..
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'iphone', 'Poco', 'Motorola'} my_Set3 = my_Set1.intersection(my_Set2) print(my_Set3)
- using symmetric_difference() method. It will return a new set, that contains only the elements that are NOT present in both sets.
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'iphone', 'Poco', 'Motorola'} my_Set3 = my_Set1.symmetric_difference_update(my_Set2) print(my_Set3)
- using symmetric_difference_update() method. It will keep only the elements that are NOT present in both sets.
my_Set1 = {'iphone', 'samsung', 'lg'} my_Set2 = {'iphone', 'Poco', 'Motorola'} my_Set1.symmetric_difference_update(my_Set2) print(my_Set1)