Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
A set is an unordered collection of unique elements in Python. Sets can be used to perform various operations like membership testing, intersection, union, and difference. This tutorial covers the basics of sets, creating sets, and common set operations.
Sets can be created in two ways:
{}
and separating elements with commas:my_set = {1, 2, 3, 4, 5} print(my_set) # Output: {1, 2, 3, 4, 5}
set()
constructor and passing an iterable (e.g., a list or tuple) as an argument:my_list = [1, 2, 3, 4, 5] my_set = set(my_list) print(my_set) # Output: {1, 2, 3, 4, 5}
Note that sets do not allow duplicate elements, so any duplicates in the input will be removed:
my_set = {1, 2, 3, 4, 5, 5, 4, 3} print(my_set) # Output: {1, 2, 3, 4, 5}
You can use the add()
method to add a single element to a set:
my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
To add multiple elements, use the update()
method and pass an iterable as an argument:
my_set = {1, 2, 3} my_set.update([4, 5, 6]) print(my_set) # Output: {1, 2, 3, 4, 5, 6}
You can use the remove()
method to remove an element from a set. However, it will raise a KeyError if the element is not found. Alternatively, you can use the discard()
method, which does not raise an error if the element is not found:
my_set = {1, 2, 3, 4, 5} my_set.remove(5) my_set.discard(6) print(my_set) # Output: {1, 2, 3, 4}
union()
method or the |
operator:set_a = {1, 2, 3} set_b = {3, 4, 5} union_set = set_a.union(set_b) # Alternatively: union_set = set_a | set_b print(union_set) # Output: {1, 2, 3, 4, 5}
intersection()
method or the &
operator:set_a = {1, 2, 3} set_b = {3, 4, 5} intersection_set = set_a.intersection(set_b) # Alternatively: intersection_set = set_a & set_b print(intersection_set) # Output: {3}
Creating and initializing sets in Python:
{}
or the set()
constructor.# Using curly braces my_set = {1, 2, 3} # Using set() constructor another_set = set([3, 4, 5])
Common set operations in Python:
set1 = {1, 2, 3} set2 = {3, 4, 5} # Union union_set = set1.union(set2) # Intersection intersection_set = set1.intersection(set2) # Difference difference_set = set1.difference(set2)
Adding and removing elements from sets in Python:
add()
and removed using remove()
or discard()
.my_set = {1, 2, 3} # Adding elements my_set.add(4) # Removing elements my_set.remove(2)
Set union, intersection, and difference in Python:
|
, &
, -
).set1 = {1, 2, 3} set2 = {3, 4, 5} # Union union_set = set1 | set2 # Intersection intersection_set = set1 & set2 # Difference difference_set = set1 - set2
Checking membership in sets in Python:
in
to check if an element is present in a set.my_set = {1, 2, 3} # Membership check is_present = 2 in my_set
Working with frozen sets in Python:
my_frozen_set = frozenset([1, 2, 3])
Set comprehension in Python:
squares_set = {x**2 for x in range(1, 6)}
Sorting and converting sets in Python:
my_set = {3, 1, 4, 1, 5, 9} # Convert to sorted list sorted_list = sorted(my_set) # Set comprehension with sorting squares_set = {x**2 for x in sorted(my_set)}