A Python set is the collection of unordered items. Each element in the set must be unique, immutable and the sets romove the duplicate elements. Sets are mutable which means we can modify it after its creation.
Unlike other collection in Python, there is no index attached to the elements of a set. Means we cannot directly access any element of a set by index. However, we can print them together or we can get the list of the elements by looping through the set.
Creating a Set:
The Sets are created by immutable items, separated from each other by comma, enclosed by curly braces{}. Python also provides the set() method, which can be used to create the set by the passed sequence.
Example 1: Using curly braces
Days={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} print(Days)
|
---|
Output:
{'Monday', 'Sunday', 'Thursday', 'Friday', 'Saturday', 'Tuesday', 'Wednesday', } <class 'set'> Monday Sunday Thursday Friday Saturday Tuesday Wednesday |
---|
Example 2: Using set() method
Days=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) print(Days)
|
---|
Output:
{'Sunday', 'Monday', 'Saturday', 'Wednesday', 'Tuesday', 'Friday', 'Thursday'} <class 'set'> Sunday Monday Saturday Wednesday Tuesday Friday Thursday |
---|
It can contain any type of element such as integer, float, tuple etc. But mutable elements (list, dictionary, set) can't be a member of set. consider the following example:
#creating a set which have immutable elements set1={1, 2, 3, "python", 6.76, 89}
|
---|
Output
<class 'set'> Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list' Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'set2' is not defined. |
---|
#empty curly braces will create dictionary set3={}
|
---|
Output:
<class 'dict'> <class 'set'> |
---|
set5={1, 2, 2, 3, 4, 4, 4, 5, 7, 7}
|
---|
Output:
Return set with unique element {1, 2, 3, 4, 5, 7} |
---|
In above example, we can see that set5 consists of many duplicate values but when we printed it removed all the duplicates from the set.
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nAdding other days to the set..."); weeks.add("Friday"); weeks.add("Saturday"); print("\nPrinting the modified set..."); print(weeks) print("\nLooping through the set elements...") for i in weeks: print(i) |
---|
Output:
{'Monday', 'Sunday', 'Wednesday', 'Thursday', 'Tuesday'} Adding other days to the set... Printing the modified set...
|
---|
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nUpdating the original set..."); weeks.update(["Friday", "Saturday"]); print("\nPrinting the modified set..."); print(weeks) print("\nLooping through the set elements...") for i in weeks: print(i) |
---|
Output:
{'Sunday', 'Thursday', 'Tuesday', 'Monday', 'Wednesday'} Adding other days to the set... Printing the modified set...
|
---|
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nRemovind some days from the set..."); weeks.discard("Monday"); weeks.discard("Wednesday"); print("\nPrinting the modified set..."); print(weeks) print("\nLooping through the set elements...") for i in weeks: print(i) |
---|
Output:
{'Monday', 'Sunday', 'Wednesday', 'Thursday', 'Tuesday'} Removing some days from the set... Printing the modified set...
|
---|
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nRemovind some days from the set..."); weeks.remove("Monday"); weeks.remove("Wednesday"); print("\nPrinting the modified set..."); print(weeks) print("\nLooping through the set elements...") for i in weeks: print(i) |
---|
Output:
{'Monday', 'Sunday', 'Wednesday', 'Thursday', 'Tuesday'} Removing some days from the set... Printing the modified set...
|
---|
pop() method:
We can also use the pop() method to remove the item.
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nRemovind some days from the set..."); weeks.pop(); weeks.pop(); print("\nPrinting the modified set..."); print(weeks) |
---|
Output:
{'Monday', 'Wednesday', 'Thursday', 'Tuesday', 'Sunday'} Removing some days from the set... 'Monday' 'Wednesday' Printing the modified set...
|
---|
weeks=set(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
print("\nRemovind all the days from the set..."); weeks.clear() print("\nPrinting the modified set..."); print(weeks) |
---|
Output:
{'Monday', 'Wednesday', 'Thursday', 'Tuesday', 'Sunday'} Removing all the days from the set... Printing the modified set...
|
---|
set1={"a", "b", "c"}
print(set3) |
---|
Output:
{'a', 1, 2, 3, 'b', 'c'} |
---|
set1={"a", "b", "c"}
print(set3) |
---|
Output:
{'a', 1, 2, 3, 'b', 'c'} |
---|
set1={"a", "b", "c"}
print(set5) |
---|
Output:
{1, 2, 3, 'desire', 'banana', 'apple', 'a', 'swift', 'b', 'c'} |
---|
set1={"a", "b", "c"}
print(set5) |
---|
Output:
{'apple', 1, 'c', 2, 3, 'desire', 'a', 'banana', 'swift', 'b'} |
---|
Join a Set and a Tuple:
set_={"a", "b", "c"}
|
---|
Output:
{'b', 1, 2, 3, 'a', 'c'} |
---|
set1={"a", "b", "c"}
|
---|
Output:
{'b', 1, 2, 3, 'a', 'c'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{'Uganda', 'Argentina'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{'USA', 'Canada'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{'USA', 'Canada'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{Canada', 'USA'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{Canada', 'Malasia', 'USA', 'Chile'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{Canada', 'Malaysia', 'USA', 'Chile'} |
---|
set1={"USA", "Canada", "Uganda", "Argentina"}
|
---|
Output:
{'USA', 'Chile', Canada', 'Malaysia'} |
---|
- = = checks if the two sets have same elements, regardless to their order.
- != checks if two sets are not equal.
- < checks if the left set is the proper subset of the right set.(i.e. All elements in the left set are also in the right set but there are also some additional elements in the right set).
- <= checks if the left set is the subset of the right set.(i.e. All elements in the left set are also in the right set).
- > checks if the left set is the proper superset of the right set.(i.e. All elements in the right set are also in the left set but there are also some additional elements in the left set).
- >= checks if the left set is the superset of the right set.(i.e. All elements in the right set are also in the left set).
month1={"January", "February", "March", "April"}
|
---|
Output:
True False False |
---|
In above example, in first case, month1 is superset of month2, so prints True. In second case month1 is not subset of month2, so prints false. In third case month2 and month3 are not equivalent so prints false.
fset=frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
---|
Output:
<class 'frozenset'> 1 2 3 4 5 6 7 8 9 |
---|
fset=frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
---|
Output:
<class 'frozenset'> Now we are trying to add the content in frozen set... AttributeError: 'frozenset' object has no attribute 'add' |
---|
FrozenSet for the dictionary:
Dictionary={"Name":"Abraham", "Country":"Jakarta", "EmpId":101} print(type(Dictionary)) print(Dictionary) fset=frozenset(Dictionary) print(type(fset)) print("\nFrozenset will take only key element from dictionary...") for i in fset: print(i) |
---|
Output:
<class 'dict'> {'Name':'Abraham', 'Country':'Jakarta','EmpId':101} <class 'frozenset'> Frozenset will take only key element from dictionary... EmpId Name Country |
---|
my_set={2, 4, 6, 8, 10, 12, 13, 14, 16, 18, 20} n=int(input("Enter the number you want to remove - ")) my_set.discard(n) print("After removing, my numbers are:", my_set) |
---|
Output:
Enter the number you want to remove - 13 After removing, my numbers are: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} |
---|
my_set={"even","numbers", 2, 4, 6} my_set.update(["odd", "numbers", 1, 3, 5,7]) print("separate even and odd numbers from the set", my_set) |
---|
Output:
separate even and odd numbers from the set{1, 2, 3, 4, 5, 6, 7, 'even', 'odd', 'numbers'} |
---|
even_num={2, 4, 6, 8, 10} odd_num={1, 3, 5, 7, 9} natural_num=even_num.union(odd_num) print(natural_num) |
---|
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
---|
Example4: Write program to find intersection between two sets
prime_num={1, 2, 3, 5, 7} odd_num={1, 3, 5, 7, 9} common_num=prime_num.intersection(odd_num) print(common_num) |
---|
Output:
{1, 3, 5, 7} |
---|
month1={"January", "February", "March", "April"}
print(issuperset)
|
---|
Output:
True False True False |
---|
SN |
Method |
Description |
1 |
add item() |
It adds an
item to the set. It has no effect if the item is already present in the set. |
2 |
clear() |
It deletes all the items from the set. |
3 |
copy() |
It returns
a copy of the set. |
4 |
difference_update(….) |
It modifies this set by removing all the items that are also
present in the specified sets. |
5 |
discard(item) |
It removes
the specified item from the set. |
6 |
intersection() |
It returns a new set that contains only the common elements of
both or more than two sets. |
7 |
intersection_update(….) |
It removes
the items from the original set that are not present in both or more than two
sets. |
8 |
isdisjoint(….) |
Return True if two sets have a null intersection. |
9 |
issubset(….) |
Report
whether another set contains this set. |
10 |
issuperset(….) |
Report whether this set contains another set. |
11 |
pop() |
Remove and
return an arbitrary set element that is the last element of the set. Raises
KeyError if the set is empty. |
12 |
remove(item) |
Remove an element from a set. It must be a member. If the element is
not the member of the set, it raises KeyError. |
13 |
symmetric_difference(….) |
Remove an
element from a set. It must be a member. If the element is not the member of
the set, it raises KeyError. |
14 |
symmetric_difference_update(….) |
Update a set with symmetric difference of itself and another. |
15 |
union(….) |
Return the
union of sets as a new set. |
16 |
Update() |
Update a set with the union of itself and others. |