Python - Sets
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
create a set :
set={"apple","banana","cherry"}
print(set)
====O/P====
{'apple', 'banana', 'cherry'}
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
You cannot access items in a set by referring to an index, since sets are unordered the items has no index.
But you 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.
set={"apple","banana","cherry"}
for i in set:
print(set)
====O/P====
{'apple', 'banana', 'cherry'}
{'apple', 'banana', 'cherry'}
{'apple', 'banana', 'cherry'}
Check if "banana" is present in the set :
set={"apple","banana","cherry"}
print("banana" in set)
====O/P====
True
Once a set is created, you cannot change its items, but you can add new items.
To add one item to a set use the add() method.
To add more than one item to a set use the update() method.
Using add() :
set={"apple","banana","cherry"}
set.add("orange")
print(set)
====O/P====
{'banana', 'orange', 'cherry', 'apple'}
Using update( ) :
set={"apple","banana","cherry"}
set.update(["orange","papaya","strawberry"])
print(set)
====O/P====
{'strawberry', 'cherry', 'orange', 'papaya', 'banana', 'apple'}
To determine how many items a set has, use the len( ) method.
set={"a","e","i","o","u"}
print(len(set))
====O/P====
5
To remove an item in a set, use the remove( ), or the discard( ) method.
set={"a","e","i","o","u"}
set.remove("a")
print(set)
====O/P====
{'e', 'o', 'i', 'u'}
Note: If the item to remove does not exist, remove() will raise an error.
Remove using discard( ) :
set={"a","e","i","o","u"}
set.discard("u")
print(set)
====o/p====
{'e', 'o', 'a', 'i'}
Note: If the item to remove does not exist, discard( ) will NOT raise an error.
We can also use the pop( ), method to remove an item,actually this method will remove the last item.But sets being unordered, we will not know what item gets removed.
The return value of the pop( ) method is the removed item.
set={"a","e","i","o","u"}
x=set.pop()
print(x)#removed item
print(set)# set after removal
====O/P====
o
{'u', 'i', 'a', 'e'}
Note: Sets are unordered, so when using the pop( ) method, you will not know which item that gets removed.
The clear( ) method empties the set.
set={"a","e","i","o","u"}
set.clear()
print(set)
====O/P====
set( )
The del keyword will delete the set completely.
set={"a","e","i","o","u"}
del set
print(set)
====o/p====
#it raises an error because set gets deleted
or we will get output as <class 'set'>
There are several ways to join two or more sets in Python.
we can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another:
set1={"a","e","i","o","u"}
set2={1,2,3,4,5}
set3=set1.union(set2)
print(set3)
===o/p===
{1, 2, 3, 4, 'o', 5, 'i', 'u', 'e', 'a'}
set1={"a","e","i","o","u"}
set2={1,2,3,4,5}
set1.update(set2)
print(set1)
====o/p====
{1, 2, 'e', 3, 4, 5, 'a', 'i', 'o', 'u'}
Note: Both union() and update() will exclude any duplicate items.
It is also possible to use the set() constructor to make a set.
set=(("a","e","i","o","u")) #note the double brackets
print(set)
===o/p===
('a', 'e', 'i', 'o', 'u')
Method | Description |
---|---|
add() | Adds an element to the set |
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 |
discard() | Remove the specified item |
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 |