Tutorial Playlist

Python Tutorial for Beginners


How to Install Python on Windows?

Lesson - 1

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

Lesson - 2

Getting Started With Jupyter Network

Lesson - 3

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 4

A Beginner’s Guide To Python Variables

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Learn A to Z About Python Functions

Lesson - 7

The Basics of Python Loops

Lesson - 8

Introduction to Python While Loop

Lesson - 9

Python For Loops Explained With Examples

Lesson - 10

Understanding Python If-Else Statement

Lesson - 11

Introduction to Python Strings

Lesson - 12

Everything You Need to Know About Python Slicing

Lesson - 13

All You Need To Know About Python List

Lesson - 14

Python Regular Expression (RegEX)

Lesson - 15

An Introduction to Python Threading

Lesson - 16

Objects and Classes in Python: Create, Modify and Delete

Lesson - 17

The Best Python Pandas Tutorial

Lesson - 18

A Beginner's Guide To Web Scraping With Python

Lesson - 19

A Handy Guide to Python Tuples

Lesson - 20

How to Easily Implement Python Sets and Dictionaries

Lesson - 21

Everything You Need to Know About Python Arrays

Lesson - 22

An Introduction to Matplotlib for Beginners

Lesson - 23

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 24

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 25

The Best NumPy Tutorial for Beginners

Lesson - 26

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 27

Top 10 Reason Why You Should Learn Python

Lesson - 28

The Best Ideas for Python Automation Projects

Lesson - 29

Top 50 Python Interview Questions and Answers in 2020

Lesson - 30

12 Tips On How To Become a Python Developer

Lesson - 31

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 32
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.

Our discussion will include:

  • What is Python?
  • Python sets and dictionaries: An introduction to sets
  • Different operations in a Python set
  • Removing elements from a set
  • Mathematical set operations
  • Useful set methods and functions
  • Python sets and dictionaries: An introduction to dictionaries
  • Various dictionary operations
  • Python dictionary methods and comprehension
  • Conclusion

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

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.

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://www.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



View More
  • Disclaimer