Python Sets

According to our experience, creators of Python have tried to make the sets in Python similar to the sets which are in maths so that it would be easy to understand them and this will be an advantage for you if you have studied sets before.

Sets are "unordered collection" of elements and are defined within "curly brackets {}" and the elements are separated with comma(,).

Sets are "mutable" datatypes i.e. we can modify them after their creation.


Example

mySet = {1, 2, 3, 4, 5}
yourSet = {"This", "is", "your", "set"}
print(mySet)
print(yourSet)

Output

{1, 2, 3, 4, 5}
{'is', 'set', 'your', 'This'}

Sets contain single elements i.e. there are no duplicate elements.

We are free to declare sets with duplicate elements but Python will count only one time with no errors.


Example

mySet = {1, 2, 3, 4, 5, 1, 2, 3}
print(mySet)

Output

{1, 2, 3, 4, 5}

As, sets are unordered so the index of any element can not be known in advance i.e. one element can have different or same index values at different times.


Example

mySet = {1, 2, 3, 4, 5}
yourSet = {"This", "is", "your", "set"}
print(mySet)
print(yourSet)

Output(different times)

{1, 2, 3, 4, 5}
{'set', 'is', 'your', 'This'}

{1, 2, 3, 4, 5}
{'This', 'is', 'set', 'your'}

{1, 2, 3, 4, 5}
{'set', 'is', 'This', 'your'}


Check an element in the set


We can check whether an element in present in a set or not.

We can use classical methods like using for loop or while loop and running it until the element doesn’t match with any element in the set.

But, here we will use "in" keyword to check the existence of an element in a set.


Example

mySet = {1, 2, 3, 4, 5}
print(1 in mySet) # will return True if present else False
print(5 in mySet)
print(6 in mySet)
print(100 in mySet)

Output

True
True
False
False

Updating sets


We can add more items to a set using "add()" and "update()".

"add()" is used to add one element and "update()" is used to add any number of elements.


Example

mySet = {1, 2, 3, 4, 5}
mySet.add(6)
print(mySet)
mySet.update([7, 8, 9])
print(mySet)

Output

{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 7, 8, 9}

set() method


We can use "set()" method to create sets.


Example

myString = "I am a String"
myList = [1, 2, 3, 4]
myTuple = (5, 6, 7, 8)
set1 = set(myString)
set2 = set(myList)
set3 = set(myTuple)
print(set1)
print(set2)
print(set3)

Output

{'r', 'i', 'n', 'S', 'g', ' ', 'I', 'a', 'm', 't'}
{1, 2, 3, 4}
{8, 5, 6, 7}

len()


Using "len()", we can find out the length of sets i.e. the number of elements present in a set.


Example

set1 = {1, 2, 3, 4, 5}
set2 = {"I", "am", "a", "set"}
print(len(set1))
print(len(set2))

Output

5
4


clear() and del


Use "clear()" to clear the elements of a set i.e. to delete the elements inside a set.
Use "del" keyword to delete a set permanently.


Example

mySet = {1, 2, 3, 4, 5}
mySet.clear()
print(mySet)
del mySet
print(mySet)

Output

set()
Traceback (most recent call last):
  File "f:/vscode/python/fsggr.py", line 5, in <module>
    print(mySet)
NameError: name 'mySet' is not defined

remove() and discard()


Use "remove()"and"discard()"to remove an element from a set.

"remove()"will raise an error if the element we are removing does not exist in the set.

"discard()"will not raise any error if the element does not exist in the set.


Example

mySet = {1, 2, 3, 4, 5}
mySet.remove(1)
print(mySet)
mySet.discard(2)
print(mySet)

Output

{2, 3, 4, 5}
{3, 4, 5}

pop() method


We can use "pop()"method to remove the last element from a set.

But it will not be possible for you to know that which element will be removed from the set, because you know that sets are unordered.

"pop()" will return that value that has been removed from the set using the pop() method.


Example

mySet = {1, 2, 3, 4, 5}
returnValue = mySet.pop()
print(mySet)
print(returnValue)

Output

{2, 3, 4, 5}
1

Useful Methods


MethodDescription
union()Return a set containing the union of sets
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
intersection()Returns a set, that is the intersection of two other sets
isdisjoint()Returns True if two sets contain similar elements
issubset()Returns True if the set is contained in the set
issuperset()Returns True if the set contains another set
symmetric_difference()Returns a set with the symmetric differences of two sets


Read More:


  • 10 Tkinter Projects for Beginners
    Introduction Python has become one of the fastest-growing programming languages, and its applications are also increasing every day. Tkinter Projects for Beginners are one of the best ways to learn the basics of Python. Tkinter is said to be one of the most straightforward modules to start learning Python as well. This will help absolute…
  • 10 Useful Python One-Liners
    In this tutorial, we will learn about the best and most powerful Python One-Liners ranging from beginner’s level to advanced level that too with examples for better understanding. Using Python One-Liners in your programs will actually make your code more readable and precise. What exactly are Python One-Liners? One-liners in simple language are single-line codes…
  • 15 Common Coding Mistakes by Beginners
    The most common mistakes that beginners while learning to code are 1. Switching between multiple languages 2. No roadmap 3. Lone wolf programming
  • 15 Deep Learning Projects for Final year
    Introduction In this tutorial, we are going to learn about Deep Learning Projects for Final year students. It contains all the beginner, intermediate and advanced level project ideas as well as an understanding of what is deep learning and the applications of deep learning. What is Deep Learning? Deep learning is basically the subset of…
  • 20 Python Projects for Resume
    About 20 Python Projects for Resume Today, in this article on 20 Python Projects for Resume, we’ll expose you to some of the top beginners to advanced python projects for resumes. In 2022, your best investment will be learning Python through practical projects. Therefore, working on actual Python projects is essential if you wish to…


2 thoughts on “Python Sets

Comments are closed.