Updated September 20, 2023
Introduction to Python Sets
Python set is a built-in data type of Python that stores data in the form of multiple items in a single variable.
Each of the elements added will be unique, we can modify them after creating the set, and it doesn’t have an index number assigned to it. Hence we cannot access the elements just by using the index number.
Table of Content
- Introduction to Python Sets
- Create Sets in Python
- Set Operations in Python
- Set Methods in Python
- FrozenSets in Python
- Comparison of Set with Other Data Types
- Sets Comprehensions in Python
- Use Cases for Sets in Python
- Common Mistakes and Best Practices with Sets
The syntax contains a set name that is usually assigned to the set of data or elements enclosed between the curly parenthesis and separated by a delimiter, that is, a comma. Or you can also use the set() method.
Syntax:
variable_name = {"string", value, number}
Or
variable_name = set([value, value, value])
Example:
firstset = {"Johnny", "Paul", "Kevin"}
print(firstset)
Here, the first set is the variable name in which the set is stored. The curly braces {} represent the set, and since we are adding string values, double/single inverted commas are necessarily required. Commas separate the values in the set. Now, since we have seen the syntax of the set with an example in Python, let us discuss the different methods used in Python sets.
Key Highlights
- A Python set is similar to a mathematical set with additional conditions.
- A Python set is immutable, doesn’t contain duplicate values, and has no index assigned to each value like other collections.
- You can easily create the set by passing the immutable elements in curly braces. Here each of the elements separates with a comma.
- We can perform mathematical operations like intersection, union, difference, accessing the elements, and modifying the set.
Create Sets in Python
To create sets in Python, place all the elements within curly braces {}, separated by commas. A set can include unlimited items, such as integers, floats, tuples, and strings. However, mutable elements such as lists, sets, and dictionaries cannot be used as elements within a set.
1. Using Set Constructor
Certainly! Here’s an example that demonstrates the creation of a set in Python using the set() constructor:
my_set = set("EDUCBA")
print(my_set)
We create a set my_set by passing a string “EDUCBA” to the set() constructor. The constructor treats the string as an iterable and creates a set with individual characters as elements.
Output:
2. Using Curly Braces
Code:
# create a set of integer type
employee_id = {12, 14, 16, 18}
print('Employee_ID:', employee_id)
# create a set of string type
vowels = {'a', 'e', 'i', 'o', 'u'}
print('Vowel Letters:', vowels)
# create a set of mixed data types
mixed_set = {'Hello', 1, -1}
print('Set of mixed data types:', mixed_set)
Output:
Set Operations in Python
Below are the different set operations used in Python:
1. Set Union
It is a function that returns a set with all elements of the original set and the specified sets. Since it returns a set, all items will have only one appearance. The item will appear only once if two sets contain the same value.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
A3= {24, 35, 47, 56}
print(A1.union(A2, A3))
Output:
In the above screenshot, you can see the code output on execution. If you look closely, you will find all values from A1 and all unique values from the other two sets.
2. Set Intersection
It is very different from the previous method’s built-in set. In this case, only the elements common in both sets or multiple sets (in case of more than two sets) are usually returned as a set. Now let us go through an example.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
A3= {24, 35, 47, 56}
print(A1.intersection(A2, A3))
Output:
As you can see, the three sets only had two elements in common which are 24 and 35. Hence, executing the code returned a set containing only 24 and 35.
3. Set Difference
This is a very important function inset. This function returns a set which is the difference between two sets. Remember that here difference does not mean subtraction because it is the difference between the number of elements in two sets and not the values of elements. For example, set A1 – set A2 means it returns a set with elements present in A1 but not in A2, and vice versa in the case of set A2 – set A1 (present in A2 but not in A1). An explanation of the same is below with the help of an example.
Code:
A1= {24, 35, 34, 45}
A2= {24, 56, 35, 46}
print(A1.difference(A2))
print(A2.difference(A1))
Output:
In the above screenshot, if you look carefully, there is a difference between the first and second results. The first result shows the elements in A but not B, whereas the second shows elements present in B but not A.
4. Symmetric Difference
The symmetric difference of the two sets method removes the same element in both sets. You can calculate it using the ^ symbol or symmetric_difference().
Code:
#First set of elements
first_list = {20, 30, 40, 50, 60}
#Second set of elements
second_list = {50, 60, 70, 80, 90}
#Using symmetric difference() method
final_result = first_list.symmetric_difference(second_list)
print("\nFinal set of elements:")
print(final_result)
Output:
Set Methods in Python
Now let’s see methods related to the set:
1. add()
As the name suggests, it is generally used to add a new element to the set, which means you are increasing the number of elements set by one. Here one very important piece of knowledge about the set is that the element only add if it is not already present in the set assets; do not take duplicate elements. The add method also does not return any value. Let us give an example.
Code:
firstset = {"Ryan", "Edward", "Gary"}
firstset.add("Larry")
print("The new word is",firstset)
#to check the duplicate property of Set
firstset.add("Larry")
print("The new word is",firstset)
Output:
If you see the output the first time when add() function is generally used, it adds the element, and the size of the set is increased by one, as shown when we execute the first print statement, but the second time when we use add() method to add the very same element (Larry) like the first time when executing the print statement, we see the same elements getting displayed with no increase in the size of the set which means that set does not any take duplicate values.
2. update()
You have seen add() method to add the elements into the set. But the only downside about add() method is you can only add an element to the set at a time. Hence update() method comes to the rescue, which helps you add multiple elements simultaneously.
Code:
first_set = set([1, 2, 3, 4, 5])
print("The first set",first_set)
first_set.add(6)
print("Final set",first_set)
Output:
3. remove()
This function is to remove elements from the set. The elements you need to remove pass as arguments. The function removes the element if present in the set; otherwise, it returns an error. We will execute an example to check this.
Code:
firstset = {"Tyler", "Scott", "Jack"}
firstset.remove("Scott")
print(firstset)
# to check the error
firstset.remove("Henry")
Output:
If you see the screenshot above, when the code is generally executed, it removes the element “Scott” as it was present in the set, but when we try to remove” Henry”, it gives us an error as “Henry” is not present in the set.
4. discard()
This built-in method also removes elements from the set but differs from the remove method we discussed earlier. If the element is present in the set, it removes the element, but if it is present, it returns no error and normally prints the set. We will see an example of this.
Code:
firstset = {"Tyler", "Scott", "Jack"}
firstset.discard("Jack")
print(firstset)
firstset.discard("Henry")
print(firstset)
Output:
If we see the above screenshot, we can see that even though “Henry” is not present in the set, we see no error displays, unlike in the case of the remove method where an error displays.
5. clear()
As the name suggests, it removes all the elements from the set. It neither takes any parameter nor does it return any value. We have to call the clear method and execute it. Let us look at an example:
Code:
firstset = {"Tyler", "Scott", "Jack"}
print("Before clear",firstset)
firstset.clear()
print("After clear",firstset)
Output:
So, the above screenshot shows that before we had executed the clear method, the list prints with elements, and then when we executed the clear() method, all the elements get easily removed. We are only left with an empty set.
6. pop()
The pop() method generally removes an item from the set. It will always aim at removing the last element from the set. If the set has unordered items, then any items from the list are used.
Code:
list_of_days = {"Monday", "Sunday", "Tuesday", "Wednesday", "Friday"}
print("\nList of Days:")
print(list_of_days)
# Let’s use the pop() method to remove the element from the set
list_of_days.pop()
# Now print the modified set
print("\nModified set:")
print(list_of_days)
Output:
The set is ordinarily unordered, so it removed Wednesday from the list.
7. copy()
This method is to create a shallow copy of a set. The term shallow copy means that if you add new elements in the set or remove elements from the set, the original set does not change. It is the basic advantage of using the copy function. We will see an example to understand the shallow copy concept.
Code:
Veggie_set = {"Drumstick","Broccoli","Mushroom"}
new_set = Veggie_set.copy()
print(new_set)
Output:
8. difference()
Code:
Fruit_set1 = {"Apple","Guava","Cherry"}
Fruit_set2 = {"Plump","Blueberry","Guava"}
New_set = Fruit_set1.difference(Fruit_set2)
print(New_set)
Output:
As one can notice, “Guava” has been removed. The same can be done the other way:
Code:
Fruit_set1 = {"Apple","Guava","Cherry"}
Fruit_set2 = {"Plump","Blueberry","Guava"}
New_set = Fruit_set2.difference(Fruit_set1)
print(New_set)
Output:
The same element present in both, i.e., “Guava”, has been removed.
9. intersection()
This method helps in finding the common elements among more than two sets.
Syntax:
set1.intersection(set2, set3 ...etc.)
More than one set can be put in the parameters of the intersection.
Code:
Flowers_set1 = {"Rose","Lotus","Lilly"}
print(Flowers_set1)
Flowers_set2 = {"Lotus","Tulip","Rose"}
print(Flowers_set2)
New_set = Flowers_set1.intersection(Flowers_set2)
print(New_set)
Output:
One can notice how the function intersection takes out common elements (). “Lotus” and “Rose” are commonly printed elements returned as a set. If one has to take an intersection between more than one set, this is how it can be done:
Code:
Flowers_set1 = {"Rose","Lotus","Lilly"}
print(Flowers_set1)
Flowers_set2 = {"Lotus","Tulip","Rose"}
print(Flowers_set2)
Flowers_set3 = {"Sunflower","Lotus","Jasmine"}
print(Flowers_set3)
New_set = Flowers_set1.intersection(Flowers_set2, Flowers_set3)
print(New_set)
Output:
10. Issubset()
This method helps you identify whether the set is a subset of others. The method issubset() always returns the Boolean output. It will return “True” if all elements of one set exist in a specified set; else, it will return “False.”
Code:
Animal_set1 = {"Tiger","Lion"}
print(Animal_set1)
Animal_set2 = {"Lion","Cheetah","Tiger"}
print(Animal_set2)
Eval = Animal_set1.issubset(Animal_set2)
print(Eval)
Output:
As one can notice, “Animal_set1” is part of “Animal_set2”. Hence issubset() method is returning True.
11. issuperset()
This method helps you identify if elements of a set are part of other specified sets. This gives output in the form of True or False.
Code:
Colors_set1 = {"Black","White"}
print(Colors_set1)
Colors_set2 = {"White","Black","Blue"}
print(Colors_set2)
check1 = Colors_set1.issuperset(Colors_set2)
print(check1)
check2 = Colors_set2.issuperset(Colors_set1)
print(check2)
Output:
As one can notice, all elements of “Colors_set1” are part of “Colors_set2”. That means “Colors_set2” is a superset of “Colors_set1” Hence:
- Statement “Colors_set1.issuperset(Colors_set2)” returns False.
- Statement “Colors_set2.issuperset(Colors_set1)” returns True.
12. symmetric_difference()
This method helps you find the elements that are part of sets but not common to both sets.
Code:
Birds_set1 = {"Bluebird","Oriole","Budgie"}
print(Birds_set1)
Birds_set2 = {"Finch","Bluebird","Weaver"}
print(Birds_set2)
check = Birds_set2.symmetric_difference(Birds_set1)
print(check)
Output:
13. union()
This method helps you unite elements from two sets. Both sets’ common and uncommon elements are formed as a new set.
Code:
Sports_set1 = {"Cricket","FootBall","Basketball"}
print(Sports_set1)
Sports_set2 = {"Basketball","Tennis","Baseball"}
print(Sports_set2)
check = Sports_set2.union(Sports_set1)
print(check)
Output:
As one can notice, “check” is the new set formed after the union of two sets “, Sports_set1” and “Sports_set2”. The common element is: “Basketball”. Uncommon elements are: “Cricket”, “FootBall”, “Tenis”, “Basketball” and “Baseball”
All these common and uncommon elements are part of this new set “check”, which is the union of both sets. For more than one set, it can be like:
Set1.union(set2,set3..etc.)
14. isdisjoint()
This function returns a true value if it doesn’t contain any intersection values or doesn’t have any common elements. If this condition is generally satisfied, then it returns true value. Else, it will return false. Let’s understand it with an example,
Code:
A1 = {2, 3, 4}
A2 = {5, 6, 8}
print("\nWithout intersection of elements:")
print(A1.isdisjoint(A2))
A3 = {9, 1, 20}
A4 = {9, 1, 20}
print("\nWith intersection of elements:")
print(A4.isdisjoint(A3))
Output:
FrozenSets in Python
The frozen set is another built-in function of Python. It is similar to the normal set with a uniqueness of immutable elements. Hence you cannot perform accessing or modifying operations on a frozen set.
The elements of the frozen set cannot be changed after the creation. Modifying frozen sets’ contents is impossible using methods like add() or remove().
Creating a FrozenSet
To create a frozenset object, you can use the frozenset() method. This method takes an iterable sequence as input and returns a frozen set as the output.
Code:
my_set = {'a', 'b', 'c', 'd', 'e'}
fset = frozenset(my_set)
print(fset)
Output:
Now let’s check for immutable characteristics.
Code:
my_set = {'a', 'b', 'c', 'd', 'e'}
fset = frozenset(my_set)
print(fset)
fset.add('f')
Output:
Operations on FrozenSets
Code:
# Frozensets
# initialize A and B
A = frozenset([10, 20, 30, 40])
B = frozenset([30, 40, 50, 60])
# copying a frozenset
C = A.copy()
print("\nPrinting set C:")
print(C)
# union
print("\nPrinting union of two sets:")
print(A.union(B))
# intersection
print("\nPrinting intersection of two sets:")
print(A.intersection(B))
# difference
print("\nPrinting difference of two sets:")
print(A.difference(B))
# symmetric_difference
print("\nPrinting symmetric difference of two sets:")
print(A.symmetric_difference(B))
Output:
Comparison of Set with Other Data Types
1. Set vs List
Set | List |
Python sets are unordered. | Lists are Ordered. |
Sets only store unique elements. | Lists can contain duplicate elements. |
Sets don’t allow to change or replace the elements. | Changing parts is possible in Python lists. |
2. Set vs Tuple
Set | Tuple |
The set is Mutable. | Tuple is Immutable. |
Python sets are an unordered collection of items. | Tuple is an ordered collection of items. |
Sets don’t allow to change or replace the elements. | Tuples also doesn’t allow to change or replace the elements. |
3. Set vs Dictionary
Set | Dictionary |
A set is a collection that has no specific order. | A dictionary is a type of data collection that stores information in key-value pairs without any particular order. |
Sets are mutable and have no duplicate elements. | Dictionaries are mutable, and keys do not allow duplicates. |
Sets are represented in curly brackets. | Dictionaries are enclosed in curly brackets in the form of key-value pairs. |
Sets Comprehensions in Python
One powerful feature of Python sets is set comprehension, which allows you to create new sets using a concise and expressive syntax. Set comprehension is similar to list comprehension, but instead of creating a new list, it creates a new set.
Set comprehension uses a set of curly braces ({}) with an expression that defines the contents of the new set. This expression can be combined with one or more for loops and/or conditional statements to filter and transform the elements of the original set.
Example:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newSet = {element*3 for element in myList}
print("The existing list is:")
print(myList)
print("The Newly Created set is:")
print(newSet)
Output:
Use Cases for Sets in Python
- Removing Duplicates from a Sequence: A common use case of sets is to remove duplicate elements from a sequence.
- Membership Testing: We can use the membership operators “in” and “not in” to check if an element exists in a set. These operators help determine the set’s presence or absence of a specified element.
Code:
s= {1, 2, 3, "EDUCBA"}
print(s)
print(1 in s)
print('S' in s)
print(2 not in s)
Output:
- Intersection, Union, Difference, and Symmetric Difference: They can also perform common math operations such as union, intersection, difference, etc. A union of two sets is another set that contains all the values from both sets. The difference() method will return the difference between the two sets. In mathematics, we have differences such as A-B and B-A. We can obtain these operations by symmetric difference.
Common Mistakes and Best Practices with Sets
- Immutable Set Elements: There are no set guidelines when creating immutable objects, but the concept is to limit access to a class’s fields after initializing them. The Set interface within the collection framework is designed to prevent the inclusion of duplicate values.
- Usage of Sets for Performance: Sets use a bunch of memory to store a huge collection of items.
- Avoiding Indexing in Sets: To index an uncountable set, use ordinal numbers, preferably initial ordinals. Often the use of indexing makes for needless effort.
Final Thoughts
Sets in Python are a powerful tool for working with collections of unique elements. With their extensive set of operations and methods, sets enable efficient data manipulation and provide a concise and expressive way to solve various programming problems. Understanding this guide’s principles and best practices allows you to leverage sets to enhance your Python programs and optimize your data processing workflows.
Frequently Asked Questions (FAQs)
Q1. What is the difference between discard() and remove() methods?
Answer: Both discard() and remove() methods perform the same task of removing an element from the set. Even though they are similar, these two methods have a main difference. While deleting an item using the remove() method shows an error message if the item doesn’t exist. In contrast, the discard() method maintains the control flow without showing an error message.
Q2. What are the pitfalls of Python sets?
Answer: The Python sets are not in an ordered manner; hence it doesn’t provide the expected results in certain methods. You cannot modify the frozen sets; hence it reduces the code redundancy as you need to create a new set every time.
Q3. What are the types of Python sets?
Answer: There are two types of sets in Python, a set and a frozen set. Where a set is mutable in nature, and you can perform modifying operations on it. In contrast, this frozen set is immutable, and you cannot modify it after creating it.
Recommended Articles
We hope that this EDUCBA information on “Python Sets” was beneficial to you. You can view EDUCBA’s recommended articles for more information.