Set

Last updated: Apr 12, 2026

Sets


In Python, a Set is an unordered collection of data types that is iterable, mutable, unindexed and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.

Creating a set: 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'. A set cannot have mutable elements like a list or dictionary, as it is mutable.

Example:

# Creation of Set in Python
set1 = set()
print("Initial blank Set: ")
print(set1)

# Another Method to create sets in Python
my_set = {1, 2, 3}
print("\n", my_set)

# Creating a Set with the use of a String
set1 = set("LIBED")
print("\nSet with the use of String: ")
print(set1)

# Creating a Set with the use of Constructor
String = 'LIBED'
set1 = set(String)
print("\nSet with the use of an Object: ")
print(set1)

# Creating a Set with the use of a List
set1 = set(["LIBED", "PYTHON", "TUTORIAL"])
print("\nSet with the use of List: ")
print(set1)

# Creating a Set with a List of Numbers
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print("\nSet with the use of Numbers: ", set1)

# Creating a Set with a mixed type of values
set1 = set([1, 2, 'PYTHON', 4, 'LIBED', 6, 'TUTORIAL'])
print("\nSet with the use of Mixed Values", set1)

Output:

Initial blank Set:
set()

 {1, 2, 3}

Set with the use of String:
{'B', 'E', 'D', 'I', 'L'}

Set with the use of an Object:
{'I', 'L', 'D', 'E', 'B'}

Set with the use of List:
{'PYTHON', 'TUTORIAL', 'LIBED'}

Set with the use of Numbers:  {1, 2, 3, 4, 5, 6}

Set with the use of Mixed Values {'LIBED', 1, 2, 4, 6, 'TUTORIAL', 'LIBED'}

A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. Order of elements in a set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set.

Adding elements to a set using add() method: Elements can be added to the Set by using the built-in add() function. Only one element at a time can be added to the set by using add() method, loops are used to add multiple elements at a time with the use of add() method. Lists cannot be added to a set as elements because Lists are not hashable whereas Tuples can be added because tuples are immutable and hence Hashable.

Example:

# Addition of elements in a Set
set1 = set()
print("Initial blank Set: ")
print(set1)

# Adding element and tuple to the Set
set1.add(8)
set1.add(9)
set1.add((6, 7))
print("\nSet after Addition of Three elements: ")
print(set1)

Output:

Initial blank Set:
set()

Set after Addition of Three elements:
{8, 9, (6, 7)}

Adding elements to a set using update() method: For the addition of two or more elements the update() method is used. The update() method accepts lists, strings, tuples as well as other sets as its arguments. In all of these cases, duplicate elements are avoided.

Example:

# Addition of elements in a Set using Update function
set1 = set([4, 5, (6, 7)])
set1.update([10, 11])
print("\nSet after Addition of elements using Update: ")
print(set1)

Output:

Set after Addition of elements using Update:
{4, 5, (6, 7), 10, 11}

Accessing a set: Set items cannot be accessed by referring to an index, since sets are unordered and the items have no index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Example:

# Accessing of elements in a set
set1 = set(["LIBED", "PYTHON", "TUTORIAL"])
print("\nInitial set")
print(set1)

# Checking the element using in keyword
print("PYTHON" in set1)

# Accessing set using loop
thisset = {"LIBED", "PYTHON", "TUTORIAL"}
for x in thisset:
    print(x)

Output:

Initial set
{'PYTHON', 'LIBED', 'TUTORIAL'}
True
PYTHON
LIBED
TUTORIAL

Removing elements from the set using remove() method or discard() method: Elements can be removed from the Set by using the built-in remove() function but a KeyError arises if the element doesn't exist in the set. To remove elements from a set without KeyError, use discard(), if the element doesn't exist in the set, it remains unchanged.

Example:

# Deletion of elements in a Set
set1 = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing elements from Set using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)

# Removing elements from Set using Discard() method
set1.discard(8)
set1.discard(9)
print("\nSet after Discarding two elements: ")
print(set1)

Output:

Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after Removal of two elements:
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}

Set after Discarding two elements:
{1, 2, 3, 4, 7, 10, 11, 12}

Removing elements from the set using pop() method: pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. If the set is unordered then there's no such way to determine which element is popped by using the pop() function.

Example:

# Deletion of elements in a Set
set1 = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing element from the Set using the pop() method
set1.pop()
print("\nSet after popping an element: ")
print(set1)

Output:

Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after popping an element:
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Removing elements from the set using clear() method: To remove all the elements from the set, the clear() function is used.

Example:

# Removing all the elements from Set using clear() method
set1 = set([1, 2, 3, 4, 5])
print("\n Initial set: ")
print(set1)

set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)

Output:

Initial set:
{1, 2, 3, 4, 5}

Set after clearing all the elements:
set()

Frozen sets: Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. If no parameters are passed, it returns an empty frozenset.

Example:

# Python program to demonstrate working of a FrozenSet
String = ('l', 'i', 'b', 'e', 'd')
Fset1 = frozenset(String)
print("The FrozenSet is: ")
print(Fset1)

# To print Empty Frozen Set No parameter is passed
print("\nEmpty FrozenSet: ")
print(frozenset())

Output:

The FrozenSet is:
frozenset({'b', 'd', 'e', 'i', 'l'})

Empty FrozenSet:
frozenset()