A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.
Properties : Unordered , Mutable\Changeable , Does not allow Duplicate Values.
Sets are unordered and its item has no index numbers. It means Index numbers do not exist in Sets.
Set items are defined in enclosed curly braces {}. For Example:
So from the above codes , we can figure out that there is no order in sets and items of sets have no specific position because there is no index numbers associated with set items.
Indexing and Slicing in Sets:¶
Because set has no indexes , so there is no indexing or slicing in set. We cannot access any particular set item. For Example:
var_set = {1,2,3,4,5,6,7,8,9}
present = 1 in var_set
print(present)
iteration in set:¶
for loop is used to iterate over the set items. For example:
In [8]:
var_set = {1,2,3,4,5,6,7,8,9}
for i in var_set:
print(i)
Set Methods:¶
Functions that are specific to Set are known as Set methods. We will go through each method one by one.
Adding an item or a single element in a set: add()¶
By now we have read other data structures like list , tuples. To add an item or a single element in list , we would use append method , but in set we use add() method. There is not much difference between both.
add() method does not return any value and adds element in set.
Note:Because sets have no index number, elements that we add in set position themselves in random place.
Just like append() , add() method also treats parameters as a single element. so if we try to add an iterable through add() method , it will be added in set as a single element. For Example:
Hence , You can add elements of any iterable in set.
update():¶
update() method adds elements of an iterables individually in a set just like union.The only difference is it does not return any value and adds element in First set . For Example:
How to remove set items from set using methods : remove() , discard() , pop()¶
remove():¶
remove() method removes the element from set. It takes only one argument.
Parameter :
element : An element that we want to remove.
For Example:
In [7]:
set_a = {1,2,3,4,5}
set_a.remove(5)
print(set_a)
Note:remove method will throw an error if element to be removed does not exist in set.
In [9]:
set_a = {1,2,3,4,5}
set_a.remove(7) #7 does not exist in set
print(set_a)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-9-778c40fc87bb> in <module>
1 set_a = {1,2,3,4,5}
----> 2 set_a.remove(7) #7 does not exist in set
3 print(set_a)
KeyError: 7
discard():¶
discard() method removes the element from set. It takes only one argument.
Parameter :
element : An element that we want to remove.
For Example:
In [10]:
set_a = {1,2,3,4,5}
set_a.remove(5)
print(set_a)
Difference between remove() and discard():¶
remove() and discard() works in same way. The only difference is remove throws an error if value to be removed does not exist in set , while discard does not throw any error. For Example:
In [11]:
set_a = {1,2,3,4,5}
set_a.discard(7) #7 does not exist in set
print(set_a)
pop():¶
You can also use the pop() method to remove an item, but this method will remove the last item. Remember that sets are unordered, so you will not know what item that gets removed. It has no parameters.
The return value of the pop() method is the removed item.
In [12]:
set_a = {1,2,3,4,5}
set_a.pop()
print(set_a)
It also returns the deleted value from set. For Example: