Python

Sets in Python

Hello, in this tutorial, we will explain Sets in Python programming language.

1. Introduction

The Set data structure in python programming is:

  • A data structure that cannot contain the duplicates elements
  • A data structure which is unordered and un-indexed
  • Elements are immutable but the whole Set is mutable
  • There is no index attached to any element in a Set

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. Download the python from this link.

2. Sets in Python

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.

2.1 How to create Set and accessing values?

Creating a set is as simple as putting different comma-separated values in the curly brackets. To access values in the set we can either use the loop or the print(…) function directly. Let us understand this with the help of a code snippet.

Creating a Set and Accessing it

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
# Creating a car set literal
# A set in python programming is a collection which is unordered and un-indexed
cars = {'BMW', 'Honda', 'Audi', 'Mercedes', 'Honda', 'Toyota', 'Ferrari', 'Tesla'}
# 2. Accessing the values from the set
# Approach1 -
print('Approach #1= ', cars)
print('==========')
# Approach2 -
print('Approach #2')
for car in cars:
    print('Car name = {}'.format(car))
print('==========')

If everything goes well the following output will be shown in the IDE console.

01
02
03
04
05
06
07
08
09
10
Approach #1=  {'BMW', 'Ferrari', 'Honda', 'Toyota', 'Audi', 'Mercedes', 'Tesla'}
==========
Approach #2
Car name = BMW
Car name = Ferrari
Car name = Honda
Car name = Toyota
Car name = Audi
Car name = Mercedes
Car name = Tesla

2.2 Adding an element to Set

To add an element to a set we use the add() method in python programming. Let us understand this with the help of a code snippet.

Adding an element

1
2
3
4
# 3. Adding an element to the set and accessing it
# There is no specific index attached
cars.add('Tata')
print('New cars set = {}'.format(cars))

If everything goes well the following output will be shown in the IDE console.

1
New cars set = {'BMW', 'Ferrari', 'Honda', 'Toyota', 'Tata', 'Audi', 'Mercedes', 'Tesla'}

2.3 Deleting an element from Set

To remove an element from a set we use the discard() method in python programming. Let us understand this with the help of a code snippet.

Removing an element

1
2
3
4
# 4. Removing an element from the set
# There is no specific index attached
cars.discard('Mercedes')
print('discard() method = {}'.format(cars))

If everything goes well the following output will be shown in the IDE console.

1
discard() method = {'BMW', 'Ferrari', 'Honda', 'Toyota', 'Tata', 'Audi', 'Tesla'}

2.4 Some basic functions

  • Union operation produces a new set containing all the distinct elements from both the sets
  • Intersection operation produces a new set containing only common elements from both the sets
  • Difference operation produces a new set containing only the elements from the first set and none from the second set
  • Compare operation checks if a given set is a subset or superset of another set. The result is true or false depending on the elements present in the sets

Let us understand this with the help of a code snippet.

Basic functions

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 5. Basic functions over set
oddDays = {"Mon", "Wed", "Fri"}
allDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
# 5(a). Union of sets
# Union operation produces a new set containing all the distinct elements from both the sets
union = oddDays | allDays
print("Union of sets = {}".format(union))
print('==========')
# 5(b). Intersection of sets
# Intersection operation produces a new set containing only common elements from both the sets
intersection = oddDays & allDays
print("Intersection of sets = {}".format(intersection))
print('==========')
# 5(c). Difference of sets
# Difference operation produces a new set containing only the elements from the first set and none from the second set
difference = allDays - oddDays
print("Difference of sets = {}".format(difference))
print('==========')
# 5(d). Compare sets
# Compare operation checks if a given set is a subset or superset of the another set
# The result is true or false depending on the elements present in the sets
subset = oddDays <= allDays
superset = allDays >= oddDays
print("Subset result = {}".format(subset))
print("Superset result = {}".format(superset))

If everything goes well the following output will be shown in the IDE console.

1
2
3
4
5
6
7
8
Union of sets = {'Sat', 'Fri', 'Wed', 'Mon', 'Thu', 'Tue', 'Sun'}
Intersection of sets = {'Wed', 'Fri', 'Mon'}
Difference of sets = {'Tue', 'Sat', 'Thu', 'Sun'}
Subset result = True
Superset result = True

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned about the:

  • A set data structure in python programming
  • Practical examples of using Set in python programming

You can download the sample class from the Downloads section.

4. Download the Project

This was an example of a Set data structure in python programming.

Download
You can download the full source code of this example here: Sets in Python

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button