Python Sets
Python Sets
Python Sets
Last Updated: 20-11-2019
In Python, Set is an unordered collection of data type that is iterable, mutable and has no
duplicate elements. The order of elements in a set is unde ned 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 speci c 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’.
Note – A set cannot have mutable elements like a list, set or dictionary, as its elements.
# Creating a Set
set1 = set()
print("Intial blank Set: ")
print(set1)
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 unde ned 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.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Output:
https://www.geeksforgeeks.org/python-sets/?ref=lbp 2/12
8/18/2020 Python Sets - GeeksforGeeks
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.
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.
# Creating a Set
set1 = set()
print("Intial blank Set: ")
print(set1)
Output:
https://www.geeksforgeeks.org/python-sets/?ref=lbp 3/12
8/18/2020 Python Sets - GeeksforGeeks
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.
Output:
Accessing a Set
Set items cannot be accessed by referring to an index, since sets are unordered the items
has no index. But you can loop through the set items using a for loop, or ask if a speci ed
value is present in a set, by using the in keyword.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 4/12
8/18/2020 Python Sets - GeeksforGeeks
# Creating a set
set1 = set(["Geeks", "For", "Geeks"])
print("\nInitial set")
print(set1)
Output:
Initial set:
{'Geeks', 'For'}
Elements of set:
Geeks For
True
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.
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
print("Intial Set: ")
print(set1)
# Removing
We elements
use cookies to fromtheSet
ensure you have best browsing experience on our website. By using our Got It !
#site,
using Remove() method
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 5/12
8/18/2020 Python Sets - GeeksforGeeks
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)
Output:
Intial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
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.
Note – If the set is unordered then there’s no such way to determine which element is
popped by using the pop() function.
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
We use cookies to 7,
ensure
8, you
9,have
10,the11,
best 12])
browsing experience on our website. By using our
Got It !
site, you acknowledge
print("Intial that you
Set: ")have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 6/12
8/18/2020 Python Sets - GeeksforGeeks
print(set1)
Output:
Intial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
To remove all the elements from the set, clear() function is used.
#Creating a set
set1 = set([1,2,3,4,5])
print("\n Initial set: ")
print(set1)
Output:
Initial set:
{1, 2, 3, 4, 5}
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 modi ed at any time, elements of the frozen set remain the same
after creation.
If no parameters are passed, it returns an empty frozenset.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 7/12
8/18/2020 Python Sets - GeeksforGeeks
# Creating a Set
String = ('G', 'e', 'e', 'k', 's', 'F', 'o', 'r')
Fset1 = frozenset(String)
print("The FrozenSet is: ")
print(Fset1)
Set Methods
FUNCTION DESCRIPTION
intersection()
We Returns
use cookies to ensure you have the best browsing the intersection
experience on our website.of
Bytwo
usingsets
our as a new set
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 8/12
8/18/2020 Python Sets - GeeksforGeeks
Set Programs
Useful Links
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Output of Python programs – Sets
https://www.geeksforgeeks.org/python-sets/?ref=lbp 9/12
8/18/2020 Python Sets - GeeksforGeeks
Recommended Posts:
Sets in Python
Python | remove() and discard() in Sets
Output of Python Programs | Set 24 (Sets)
Python | sympy.sets.open() method
Python | sympy.sets.Lopen() method
Python | sympy.sets.Ropen() method
Python Set | Pairs of complete strings in two sets
Python program to count number of vowels using sets in given string
Python program to nd common elements in three lists using sets
Cartesian Product of any number of sets
Reusable piece of python functionality for wrapping arbitrary blocks of code : Python
Context Managers
Python - Read blob object in python using wand library
Python | Index of Non-Zero elements in Python list
Python | PRAW - Python Reddit API Wrapper
MySQL-Connector-Python module in Python
Important differences between Python 2.x and Python 3.x with examples
Python | Merge Python key values to list
Reading Python
We use cookies File-Like
to ensure Objects
you have the bestfrom C | experience
browsing Python on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 10/12
8/18/2020 Python Sets - GeeksforGeeks
15
1.6
To-do Done
Based on 6 vote(s)
Please write to us at [email protected] to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
Company Learn
We use cookies to ensure you have the best browsing experience on our website. By using our
About Us Algorithms Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Careers Data Structures
https://www.geeksforgeeks.org/python-sets/?ref=lbp 11/12
8/18/2020 Python Sets - GeeksforGeeks
Ca ee s a a S uc u es
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/python-sets/?ref=lbp 12/12