How to Use Python Dictionaries

August 19, 2022
Published by

Discover how the Python dictionary data structure functions as a strong and flexible way to store and retrieve objects and data in your applications.

Python is a high-level, interpreted, object-oriented programming language that allows us to build a wide range of applications. “Data Dictionaries” are one of the many different forms of data structures. 

What is a Data Dictionary?

Dictionaries are powerful data structures in Python that store data as key pairs, this being represented in the following form: Key-Value. Dictionary comprehension (or dict comprehension) can be very useful in creating new dictionaries based on existing and iterable dictionaries.

Python dictionaries are made up of one or more keys and the values that go with them. Any Python object can serve as the value for each key. This kind of data structure is sometimes referred to as an associative array or a hash map in other languages. It employs a key to get the values that are associated with it, and the lookup time is fixed.

We will go through the fundamentals of Python dictionaries in this post, including how to use them, the situations in which they make sense, and some typical issues and traps to watch out for.

The dictionary’s components are all encased in a pair of square brackets.

Here is  a structure appropriate for a dictionary:

python dictionaries

In the previous example,

“key1” through “key4” are the four keys.

How to Build a Python Dictionary From an Iteration Using Dictionary Compression

In this part, we’ll utilize dictionary compression to turn a list or tuple into a Python dictionary.

If we decide to produce either the keys or the values instantly, we may build a new dictionary in Python using simply an iterable.

The iterable’s items can be used as keys and vice versa depending on how we decide to produce the values in question.

Below is a general illustration of syntax. You should be aware that the names in brackets (>) are placeholders for the variable names.

python dictionaries

Let’s examine the syntax shown above.

Understanding Python Dictionaries: Example 

Let’s say we have a list of customers that come into our business, and we want to give each one a random discount. Additionally, we would like the discount amount to range from $1 to $100.

In Python, random.randint(i,j) returns a value between i and j including both extremes.

As a result, we may create a discount between $1 and $100 for each consumer in the list using the randint() method.

The code below will demonstrate how to build a discount dictionary from a list of clients.

python dictionaries

The aforementioned example operates as follows:

How to Utilize a Python Dictionary’s Items() Function

We have already shown how to access values using keys. But how can we have access to all of a dictionary’s key-value pairs?

We utilize the dictionary’s items() function to retrieve all the key-value pairs to do this.

python dictionaries

How to Build a Python Dictionary From an Existing Dictionary Using Dictionary Understanding

Let’s now consider the scenario in which we already own a Python dictionary.

However, we want to make a new dictionary that only has the words from the current one that meets a specific requirement.

Now let’s go through the syntax. We retrieve every key-value pair in the current dictionary using the items() function.

Python Dictionaries – Example 2

Recall the bargain dictionary from our earlier illustration.

We can observe that some customers were fortunate enough to receive more discounts than others. Let’s investigate this discrepancy and please every client.

People who have received a discount of less than $30 are eligible for a 10% discount on their subsequent purchase.

We are building a new dictionary clientele 10 in this instance using our discount dictionary.

The code above explains the following:

The updated lexicon now includes Alex, Bob, and Dave who each received discounts totaling less than $30.

The Synopsis of Python Dictionaries

Let’s review everything we discovered during this session. By building new dictionaries in Python, we learned how to utilize dictionary comprehension. belonging to a single iterable and an existing dictionary that filters over the contents using conditionals.

Related Posts
Dedicated Software Development Team

How Teravision Technologies can set up a Dedicated Software Development Team in 48 hours: “The Bench”

  • Digital Agencies
  • Software Development
  • . . .
Nearshore Software Development company in Colombia

Teravision Technologies opens new Nearshore Software Development center in Bogotá, Colombia.

  • Agile
  • Software Development
  • . . .


Nearshore software outsourcing that just makes sense.

We will follow up with you soon

Try us at 1 888 9898324 or send an email to