Last Updated : 09 Jul, 2025
Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates. The order of elements in a set is not preserved and can change.
Creating a Set in PythonIn Python, the most basic and efficient method for creating a set is using curly braces.
Example:
Python
set1 = {1, 2, 3, 4}
print(set1)
Using the set() function
Python Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a 'comma'.
Note: A Python set cannot contain mutable types such as lists or dictionaries, because they are unhashable.
Example:
Python
set1 = set()
print(set1)
set1 = set("GeeksForGeeks")
print(set1)
# Creating a Set with the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print(set1)
# Creating a Set with the use of a tuple
tup = ("Geeks", "for", "Geeks")
print(set(tup))
# Creating a Set with the use of a dictionary
d = {"Geeks": 1, "for": 2, "Geeks": 3}
print(set(d))
set() {'e', 'r', 'o', 'k', 'G', 's', 'F'} {'For', 'Geeks'} {'for', 'Geeks'} {'for', 'Geeks'}Unordered, Unindexed and Mutability
In set, the order of elements is not guaranteed to be the same as the order in which they were added. The output could vary each time we run the program. Also the duplicate items entered are removed by itself.
Sets do not support indexing. Trying to access an element by index (set[0]) raises a TypeError.
We can add elements to the set using add(). We can remove elements from the set using remove(). The set changes after these operations, demonstrating its mutability. However, we cannot changes its items directly.
Example:
Python
set1 = {3, 1, 4, 1, 5, 9, 2}
print(set1) # Output may vary: {1, 2, 3, 4, 5, 9}
# Unindexed: Accessing elements by index is not possible
# This will raise a TypeError
try:
print(set1[0])
except TypeError as e:
print(e)
{1, 2, 3, 4, 5, 9} 'set' object is not subscriptableAdding Elements to a Set in Python
We can add items to a set using add() method and update() method. add() method can be used to add only a single item. To add multiple items we use update() method.
Example:
Python
# Creating a set
set1 = {1, 2, 3}
# Add one item
set1.add(4)
# Add multiple items
set1.update([5, 6])
print(set1)
{1, 2, 3, 4, 5, 6}Accessing a Set in Python
We can loop through a set to access set items as set is unindexed and do not support accessing elements by indexing. Also we can use in keyword which is membership operator to check if an item exists in a set.
Example:
Python
set1 = set(["Geeks", "For", "Geeks."])
# Accessing element using For loop
for i in set1:
print(i, end=" ")
# Checking the element# using in keyword
print("Geeks" in set1)
Geeks For Geeks. True
Explanation:
We can remove an element from a set in Python using several methods: remove(), discard() and pop(). Each method works slightly differently :
remove() method removes a specified element from the set. If the element is not present in the set, it raises a KeyError. discard() method also removes a specified element from the set. Unlike remove(), if the element is not found, it does not raise an error.
Python
# Using Remove Method
set1 = {1, 2, 3, 4, 5}
set1.remove(3)
print(set1)
# Attempting to remove an element that does not exist
try:
set1.remove(10)
except KeyError as e:
print("Error:", e)
# Using discard() Method
set1.discard(4)
print(set1)
# Attempting to discard an element that does not exist
set1.discard(10) # No error raised
print(set1)
{1, 2, 4, 5} Error: 10 {1, 2, 5} {1, 2, 5}Using pop() Method
pop() method removes and returns an arbitrary element from the set. This means we don't know which element will be removed. If the set is empty, it raises a KeyError.
PythonNote: If the set is unordered then there's no such way to determine which element is popped by using the pop() function.
set1 = {1, 2, 3, 4, 5}
val = set1.pop()
print(val)
print(set1)
# Using pop on an empty set
set1.clear() # Clear the set to make it empty
try:
set1.pop()
except KeyError as e:
print("Error:", e)
1 {2, 3, 4, 5} Error: 'pop from an empty set'Using clear() Method
clear() method removes all elements from the set, leaving it empty.
Python
set1 = {1, 2, 3, 4, 5}
set1.clear()
print(set1)
Frozen Sets in Python
A frozenset in Python is a built-in data type that is similar to a set but with one key difference that is immutability. This means that once a frozenset is created, we cannot modify its elements that is we cannot add, remove or change any items in it. Like regular sets, a frozenset cannot contain duplicate elements.
If no parameters are passed, it returns an empty frozenset.
Python
# Creating a frozenset from a list
fset = frozenset([1, 2, 3, 4, 5])
print(fset)
# Creating a frozenset from a set
set1 = {3, 1, 4, 1, 5}
fset = frozenset(set1)
print(fset)
frozenset({1, 2, 3, 4, 5}) frozenset({1, 3, 4, 5})Typecasting Objects into Sets
Typecasting objects into sets in Python refers to converting various data types into a set. Python provides the set() constructor to perform this typecasting, allowing us to convert lists, tuples and strings into sets.
Python
# Typecasting list into set
li = [1, 2, 3, 3, 4, 5, 5, 6, 2]
set1 = set(li)
print(set1)
# Typecasting string into set
s = "GeeksforGeeks"
set1 = set(s)
print(set1)
# Typecasting dictionary into set
d = {1: "One", 2: "Two", 3: "Three"}
set1 = set(d)
print(set1)
{1, 2, 3, 4, 5, 6} {'f', 'G', 's', 'k', 'r', 'e', 'o'} {1, 2, 3}Advantages of Set in Python
Overall, sets can be a useful data structure in Python, especially for removing duplicates or for fast membership testing. However, their lack of ordering and limited functionality can also make them less versatile than lists or dictionaries, so it is important to carefully consider the advantages and disadvantages of using sets when deciding which data structure to use in your Python program.
Set Methods in Python Function Description add() Adds an element to a set remove() Removes an element from a set. If the element is not present in the set, raise a KeyError clear() Removes all elements form a set copy() Returns a shallow copy of a set pop() Removes and returns an arbitrary set element. Raise KeyError if the set is empty update() Updates a set with the union of itself and others union() Returns the union of sets in a new set difference() Returns the difference of two or more sets as a new set difference_update() Removes all elements of another set from this set discard() Removes an element from set if it is a member. (Do nothing if the element is not in set) intersection() Returns the intersection of two sets as a new set intersection_update() Updates the set with the intersection of itself and another isdisjoint() Returns True if two sets have a null intersection issubset() Returns True if another set contains this set issuperset() Returns True if this set contains another set symmetric_difference() Returns the symmetric difference of two sets as a new set symmetric_difference_update() Updates a set with the symmetric difference of itself and another Recent Articles on Python Sets Set ProgramsRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4