Python Set:
A set is a collection of unordered and unindexed elemests.
In Python sets are written with curly brackets.
Example:
set1 = {"pen", "book", "copy"}
print(set1)
print(set1)
Output:
{"book", "copy", "pen"}
Note: The set is not in ordered which means items will appear in a random order whenever you execute the program again.
Adding element in set:
Note: Once a set is created you cannot change its items but you can add new or update the items.
To add one item to a set use the
add()
method.Example:
set1 = {"pen", "book", "copy"}
set1.add("pencil")
print(set1)
Output:
{"pen","pencil","book","copy"}
Set update( ) method:
To add more than one item to a set use the
update()
method.Example:
set1 = {"pen", "book", "copy"}
set1.update(["pencil","eraser"])
print(set1)
Output:
{"pen", "book","eraser","pencil", "copy"}
To find length of a Set:
To find the length, use the len()
method.
Example:
set1 = {"pen", "book", "copy"}
print(len(set1))
Output:
3
To remove item, use the remove()
method or discard() method.
Note: The only difference between remove() and discard() is that, if the item to remove does not exist the remove() will raise error but discard() will not.
Example:
set1 = {"pen", "book", "copy"}
set1.remove("book")
print(set1)
Output:
{"pen","copy"}
Set pop( ) method:
To remove item from last, use the pop()
method but as we know that sets are unordered so we do not know that which element will pop out.
Example:
set1 = {"pen", "book", "copy"}
x = set1.pop()
print(x)
print(set1)
Output:
"book"
{"pen", "copy"}
To delete complete Set:
To delete the set completely, use the del()
method.
Example:
set1 = {"pen", "book", "copy"}
del set1
print(set1)
Output:
Traceback (most recent call last):
File "demo_set_del.py", line 5, in <module>
print(set1) #this will raise an error because the set no longer exists
NameError: name 'set1' is not defined
File "demo_set_del.py", line 5, in <module>
print(set1) #this will raise an error because the set no longer exists
NameError: name 'set1' is not defined
To clear the Set:
To clear the set completely, use the clear()
method.
Example:
set1 = {"pen", "book", "copy"}
set1.clear()
print(set1)
Output:
set1()
To create a Set:
Example:
set1 = {"pen", "book", "copy"}
set1.clear()
print(set1)
Output:
set1()
To create a Set:
set1()
To create a Set:
To create a set we use set() constructor.
Example:
set1 = set(("pen", "book", "copy"))
print(set1)
Output:
{"pen", "book", "copy"}
To copy one set into another:
The
copy()
method copies one set into another.
Example:
set1 = {"pen", "book", "copy"}
x = set1.copy()
print(x)
Output:
{"pen", "book", "copy"}
To take out difference between to sets:
The
difference()
method returns a set that contains the difference between two sets.Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","pen"}
set2 = {"eraser","scale","pen"}
x = set1.difference(set2)
print(x)
Output:
{"book", "copy"}
To remove item that exist in both sets:
The
difference_update()
method removes the items that exist in both sets.
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","pen"}
set2 = {"eraser","scale","pen"}
set1.difference_update(set2)
print(set1)
Output:
{"book", "copy"}
Note: The
difference_update()
method is different from difference()
method, because the difference()
method returns a new set, without the unwanted items, and the difference_update()
method removes the unwanted items from the original set and also update the original set.
To check subset of a set:
The
issubset()
method returns True if all items in the set exists in the other set, otherwise it retuns False.
Example:
set1 = {"a", "b", "c"}
set2 = {"d", "f", "e", "c", "b", "a"}
set2 = {"d", "f", "e", "c", "b", "a"}
x = set1.issubset(set2)
print(x)
Output:
True
Set union method:
The
You can specify as many sets you want, separated by commas.
Syntax = set.union(set1, set2...)
Example:
The
union()
method returns a set that contains all items from the original set, and all items from the specified sets, simply it add the two sets. You can specify as many sets you want, separated by commas.
Syntax = set.union(set1, set2...)
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","notebook"}
set2 = {"eraser","scale","notebook"}
x = set1.union(set2)
print(x)
Output:
{"pen", "book", "copy","eraser","scale","notebook"}
Set intersection methods:
The
Syntax: set.intersection(set1, set2 ... etc)
The
intersection()
method returns a set that contains the similarity between two or more sets.Syntax: set.intersection(set1, set2 ... etc)
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","notebook"}
set2 = {"eraser","scale","notebook"}
x = set1.intersection(set2)
print(x)
Output:
{"pen"}
Set intersection_update() methods:
The
Syntax: set.intersection_update(set1, set2 ... etc)
Set intersection_update() methods:
The
intersection_update()
method removes the items that is not present in both sets.Syntax: set.intersection_update(set1, set2 ... etc)
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","notebook"}
set2 = {"eraser","scale","notebook"}
set1.intersection_update(set2)
print(x)
Output:
{"pen"}
Note:
intersection_update()
method is different from the intersection()
method, because intersection()
method returns a new set, without the unwanted items, and the intersection_update()
method removes the unwanted items from the original set and update the original list.
Set isdisjoint() method:
The
isdisjoint()
method returns True if none of the items are present in both sets are similar, otherwise it returns False.
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","notebook"}
set2 = {"eraser","scale","notebook"}
x = set1.isdisjoint(set2)
print(x)
Output:
Set symmetric_difference() method:
The
The
symmetric_difference()
method returns a set that contains all items from both set, but not the items that are present in both sets, it means that it takes out the common elements.
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","pen"}
set2 = {"eraser","scale","pen"}
print(x)
Output:
{"book", "copy","eraser","scale"}
set1.symmetric_difference_update(set2)
{"book", "copy","eraser","scale"}
Set symmetric_difference_update() method:
The
symmetric_difference_update()
method updates the original set by removing items that are present in both sets.
It update the original set.
Example:
set1 = {"pen", "book", "copy"}
set2 = {"eraser","scale","pen"}
set2 = {"eraser","scale","pen"}
print(x)
Output:
{"book", "copy","eraser","scale"}
{"book", "copy","eraser","scale"}
0 Comments