Tutorial Playlist

Python Tutorial for Beginners


The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2022: Choosing The Best One

Lesson - 3

A Beginner’s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

Python OOPs Concept: Here's What You Need to Know

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

The Best Guide for RPA Using Python

Lesson - 34

Comprehending Web Development With PHP vs. Python

Lesson - 35

The Best Tips for Learning Python - REMOVE

Lesson - 36

The Best Way to Learn About Box and Whisker Plot

Lesson - 37

An Interesting Guide to Visualizing Data Using Python Seaborn

Lesson - 38

The Complete Guide to Data Visualization in Python

Lesson - 39

Everything You Need to Know About Game Designing With Pygame in Python

Lesson - 40

The Complete Simplified Guide to Python Bokeh

Lesson - 41

Top 150 Python Interview Questions and Answers for 2022

Lesson - 42

The Supreme Guide to Understand the Workings of CPython

Lesson - 43
How to Easily Implement Python Sets and Dictionaries

Python is one of the most popular programming languages available today. It is widely used in many business-related IT processes such as programming, web development, machine learning, and data science. Given its widespread use, it's not surprising that Python has surpassed Java as the top programming language. In this article, we will be looking at sets and dictionaries, one of the most essential elements in Python programming.

What Is Python?

Python is a general-purpose programming language often used for scripting. Although many scripting languages have limitations, Python is a simple yet robust programming language that delivers both power and complexity. Python is designed for simplicity, both in reading and writing.


Python Sets and Dictionaries: An Introduction to Sets

A Python set is an unordered collection of unique elements. Every set element is unique and immutable. A set can consist of different data types such as integers, strings, tuples, etc.

Fig: Example of Python Sets

Different Operations in a Python Set

1. How to create a Python set

In Python, sets are written within curly braces separated by commas, or by using the set() method.

Example: Using curly braces { }


Fig: Creating a Python Set using curly braces

Example: Using the set() method

Fig: Creating a Set using set() method

2. How to access and change elements in a set

We cannot access or change elements by using index numbers since sets are unordered. However, we can loop through the set to find out if the particular item exists or not. 


Fig: To check if a particular element exists in Set or not

3. How to add elements to a set?

If we want to add a single element, we use the add() method.

Syntax: set_name.add (element)



Fig: Adding single element using add() method

To add multiple elements, we use the update() method.



Fig: Add multiple elements using update() method

4. How to get the length of the set

To check how many elements a Python Set has, we use the len() method.



Fig: To check the number of elements in a Python Set

Removing Elements from a Set 

To remove an item from a set, we use remove() or discard() method.



Fig: Deleting elements using remove() and discard() method

Now that we are done adding and removing elements from a set, let’s look at set operations and methods.

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

Mathematical Set Operations

There are various operations that can be performed within a Python set. They are:

1. Set union

The union of the two sets is calculated using ( | ) operator. The union consists of all the elements from both sets.



Fig: Union of Two Sets

2. Set intersection

The intersection of two sets is calculated using ( & ) operator. The intersection consists of common elements from both the sets.



Fig: Intersection of two Sets

3. Set difference

The difference between the two sets is calculated by using ( - ) operator. 


Fig: Difference between two sets

In the output, we get only those elements that are unique to set A.

Useful Set Methods and Functions

Here are some useful methods available for the set objects.

1. clear()

Deletes all the items from the set.

Syntax : set_name.clear()



Fig: To clear all the elements in a Python Set

2. copy()

Returns a copy of the set.

Syntax: set_name.copy()



Fig: copy() method

3. difference_update()

This method updates the set, who requested the method, and what the differences are between the two sets. 



Fig: difference_update() method

4. intersection_update()

This method updates the set, who requested the method, by the intersection of two sets.



Fig: intersection_update() method

Built-in Set Functions

Python set has some in-built functions. They are:

1. max()

Returns the maximum value from the set.



Fig: max() function

2. min()

Returns the minimum value from the set.



Fig: min() function

3. sum()

Returns the total sum of all the elements.


Fig: sum() function

Now that we are done with sets, let’s focus on dictionaries.

Python Sets and Dictionaries: An Introduction to Dictionaries

A Python dictionary is a collection of key-value pairs. They are an unordered collection of items and can be changed.

Various Python Dictionary Operations

1. How to create a dictionary

One or more “key: value” pairs, separated by commas, are put inside curly brackets to form a dictionary object.

Syntax: variable_name = { key 1 : value 1, key 2 : value 2 }


Fig: To create a Python Dictionary of various data types

2. How to access elements from inside a dictionary

We can access the elements inside a dictionary by referring to its key name, inside the square brackets, or by using get() method.



Fig: To access element

3. Changing and adding dictionary elements

We can change the value of a specific item by referring to its key name.



Fig: To change a value in a Dictionary

To add a new element, we use the following syntax.

Syntax: dictionary_name [ “ key value “] = “element”


Fig: To add a new element in a Python Dictionary

Removing an Element From a Dictionary

The del() keyword removes the element of the specified key name.

Syntax: del dictionary_name [ “ key name “ ]



Fig: To delete an element in Python Dictionary

Dictionary methods

Python dictionaries have some useful built-in methods.

1. clear()

This method removes all the elements from the dictionary.



Fig: clear() method

2. fromkeys()

The fromkeys() method creates a new dictionary with a default value for all specified keys.

If the default value is not specified, all keys are set to None.

Syntax: dict.fromkeys( keys, value )



Fig: fromkeys() method

3. update()

The update() method inserts the new key-value pair.



Fig:update method()

Python Dictionary Methods and Comprehension

A comprehension is a means of creating a Python data structure from iterators. Dictionary comprehension is a way to build a new dictionary by applying an expression to each item in the iterable.

Syntax: { key: value for var in interable }



Fig: Dictionary Comprehension

Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course..


Python sets and dictionaries are very useful for constructing and gaining easy access to individual Python elements. To learn more about Python sets and dictionaries, visit this link: https://m.youtube.com/watch?v=WA_JDDaaMe0

If you have any questions or comments, please share them in the comment section of the article, and we’ll have our experts get back to you.

Want to Kickstart Your Career in Data Science?

Python is an indispensable skill for any data scientist—and data scientists are in super high demand. If you’re interested in joining this exciting field, consider enrolling in our Data Science with Python Course, which is co-developed with IBM.


About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.