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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

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 { }

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. 

Example: 

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)

Example

add

Fig: Adding single element using add() method

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

Example: 

multiple.

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.

Example:

number

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.

Example:

remove

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.

Become an Automation Test Engineer in 11 Months!

Automation Testing Masters ProgramExplore Program
Become an Automation Test Engineer in 11 Months!

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.

Example:

union

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.

Example:

intersection

Fig: Intersection of two Sets

3. Set difference

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

Example:

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()

Example: 

clear

Fig: To clear all the elements in a Python Set

2. copy()

Returns a copy of the set.

Syntax: set_name.copy()

Example: 

 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. 

Example:

difference-method

Fig: difference_update() method

4. intersection_update()

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

Example:

intersection-update

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.

Example:

max

Fig: max() function

2. min()

Returns the minimum value from the set.

Example:

min-function

Fig: min() function

3. sum()

Returns the total sum of all the elements.

Example:

Fig: sum() function

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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 }

python-dictionary

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.

Example:

access-element

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.

Example:

change-value

Fig: To change a value in a Dictionary

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

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

Example:

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 “ ]

Example:

delete-element

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.

Example:

clear-2.

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 )

Example:

fromkeys

Fig: fromkeys() method

3. update()

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

Example:

/update

Fig:update method()

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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 }

Example:

/comprehension

Fig: Dictionary Comprehension

Conclusion

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 Software Development?

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 Post Graduate Program in Full Stack Web Development. This course can help you hone the right skills and make you job-ready.

If you have any questions, feel free to post them in the comments section below. Our team will get back to you at the earliest.

 

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

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