Python Sets

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements.

Set in Python is equivalent to sets in mathematics. The order of elements in a set is undefined though it may consist of various elements. Elements of a set can be added and deleted, elements of the set can be iterated, various standard operations (union, intersection, difference) can be performed on sets. Besides that, 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 ‘comma’. 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.

Note – A set cannot have mutable elements like a list, set or dictionary, as its elements.

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program to demonstrate

# Creation of Set in Python

# Creating a Set

set1 =set()

print("Intial blank Set: ")

print(set1)

# Creating a Set with

# the use of a String

set1 =set("GeeksForGeeks")

print("\nSet with the use of String: ")

print(set1)

# Creating a Set with

# the use of Constructor

# (Using object to Store String)

String ='GeeksForGeeks'

set1 =set(String)

print("\nSet with the use of an Object: ")

print(set1)

# Creating a Set with

# the use of a List

set1 =set(["Geeks", "For", "Geeks"])

print("\nSet with the use of List: ")

print(set1)

# Creating a Set with

# a List of Numbers

# (Having duplicate values)

set1 =set([1, 2, 4, 4, 3, 3, 3, 6, 5])

print("\nSet with the use of Numbers: ")

print(set1)

# Creating a Set with

# a mixed type of values

# (Having numbers and strings)

set1 =set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])

print("\nSet with the use of Mixed Values")

print(set1)

chevron_right

filter_none

Output:

Intial blank Set:
set()
Set with the use of String:
{'e', 'r', 'k', 'o', 'G', 's', 'F'}
Set with the use of an Object:
{'r', 'o', 'e', 'F', 's', 'k', 'G'}
Set with the use of List:
{'Geeks', 'For'}
Set with the use of Numbers:
{1, 2, 3, 4, 5, 6}
Set with the use of Mixed Values
{1, 2, 4, 'Geeks', 6, 'For'}

Adding Elements to a Set

Elements can be added to the Set by using 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 whereas for addition of two or more elements 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.

Note – 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.

Removing elements from the Set

Elements can be removed from the Set by using built-in remove() function but a KeyError arises if 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. 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. To remove all the elements from the set, clear() function is used.Note – If the set is unordered then there’s no such way to determine which element is popped by using the pop() function.

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.