Python Sets

About Python Sets

In Python, a set is an unordered collection of unique elements. The elements are seperated by comma (,).

Python set is mutable (changeable). You can add or remove elements from them.

Sets are basically used to test membership and eliminate duplicate entries.

Set objects support mathematical operations like union, intersection, difference, and symmetric difference.

It is defined using curly braces {}.

Creating a set

You can create a set by enclosing a sequence of unique elements inside curly braces '{}'. Elements of set must be unique and seperated by comma (,).

In the following example, x_set = {1, 2, 3, 4, 5} is an example of set where x_set is a variable of set and {1, 2, 3, 4, 5} is the sequence of elements inside curly braces.

Please have a colser look at the following example.

Example of creating set:
Output of the above example
 {1, 2, 3, 4, 5}
                                  

Adding Elements to a Set

The 'add()' method or the 'update()' method can be used to add element to a set

add():The add() method is used to add a single element. In the first example, x_set = {1, 2, 4, 5} is a set. The code x_set.add(3) is used to add 3 to the x_set using add() method.

update():In the second example, y_set = {1, 2, 4, 5} is a set. The code y_set.update([3, 6]) is used to add elements of the list [3, 6] to the y_set using update() method. Please note that update() method takes an iterable object as argument.

Example of adding elements to sets:
Output of the above example
Using add() method: {1, 2, 3, 4, 5}
Using update() method: {1, 2, 3, 4, 5, 6}
                                

Removing elements of set

In Python, you can remove elements from a set using the remove(), discard(), or pop() method.

remove():In the first example, a_set = {1, 2, 4, 5} is a set. The code a_set.remove(2) is used to remove 2 from the set a_set using remove() method.

discard():In the second example, b_set = {1, 2, 4, 5} is a set. The code b_set.discard(5) is used to remove 5 from the set b_set using discard() method.

pop():In the third example, c_set = {1, 2, 3, 4, 5} is a set. The code returned_element = c_set.pop() is used to remove and return an arbitrary element from the set ba_set using pop() method.

Example of accessing elements of a tuple using negative indices:
Output of the above example
Set elements after using remove() method: {1, 4, 5}
Set elements after using discard() method: {1, 2, 4}
Popped element: 1
Set elements after using pop() method: {2, 3, 4, 5}
                                

Iterating through the elements of set

In Python, you can iterate through the elements of set using a for loop.

In the following example, my_set is a set variable with (1, 2, 3, 4 ,5 , 6) items. for element in my_set: is used to iterate (loop) through the set variable 'my_set'. print(element) prints the elements of set.

Example of iterating through the elements of a set:
Output of the above example
1
2
3
4
5
6
                                

Membership testing in set

In Python, you can check if an element is present in a set using the in operator.

In the following example, p_set is a set variable with (1, 2, 3, 4 ,5 , 6) items. if 3 in p_set: is used to check if 3 is present in 'p_set' set. As 3 is present in the set, the following code 'print("3 is present in the set.")' will be executed.

Example of testing membership in set:
Output of the above example
3 is present in the set.
                                

Mathematical operations in set

In Python, you can use sets to perform various mathematical operations such as union, intersection, difference, and symmetric difference.

union(): The union() method returns all unique elements from both sets. In the following example, union_set = a_set.union(b_set) is used to return all unique elements of 'a_set' and 'b_set' sets. You can also achieve the union operation using bitwise OR (|) operator e.g. a_set | b_set.

intersection(): The intersection() method returns the elements that are common to both sets. In the following example, intersection_set = a_set.intersection(b_set) is used to return the elements that are common to 'a_set' and 'b_set' sets. You can also achieve the intersection operation using bitwise AND (&) operator e.g. a_set & b_set.

difference(): The difference() method returns the elements that are in the first set but not in the second set. In the following example, difference_set = a_set.difference(b_set) is used to return the elements that are in the 'a_set' set but not in the 'b_set' set. You can also achieve the difference operation using subtraction (-) e.g. a_set - b_set.

symmetric_difference(): The symmetric_difference() method returns the elements that are present in either set, but not in both sets. In the following example, sym_dif_set = a_set.symmetric_difference(b_set) is used to return the elements that are present in either 'a_set' or 'b_set' set, but not in both sets. You can also achieve the symmetric difference operation using bitwise XOR (^) operator e.g. a_set ^ b_set.

Example of set mathematical operations:
Output of the above example
Union set: {1, 2, 3}
Intersection set: {2}
Difference set: {1}
Symmetric difference set: {1, 3}
                                

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python tuples and try different methods and functions on the elements of tuple in the following Python code editor by clicking Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output