Dictionary In Python

1. DICTIONARY 101

1.1. Defining a Dictionary

A dictionary in Python is a collection of key-value pairs.

  • A dictionary is wrapped in braces, {}

  • Every key is connected to its value by a colon :, and individual key-value pairs are separated by commas ,

  • You can use a key to access the value associated with that key

  • A key’s value can be a number, a string, a list, another dictionary or virtually any object in Python

  • You can store as many key-value pairs as you want in a dictionary

Let’s define a simple dictionary that stores some pieces of information about and store it inside a variable user_car

user_car ={'make': 'honda','model': 'fit','year': 2015}

In the above example, there are three key-value pairs. Keys are make, model and year with their associated values — honda, fit and 2015 , respectively.

1.2. Accessing Values in a Dictionary

To get the value associated with a key, give the name of the dictionary and then place the key inside a set of square brackets

print(user_car['make'].title())
print(user_car['model'].title())

1.3 Adding New Key-Value Pairs

To add a new key-value pair, you would provide the name of the dictionary followed by the new key in square brackets and assign a value. Let’s add the color and country to our previous dictionary user_car

1.4. Starting with an Empty Dictionary

Why we need an empty dictionary? One case would be when storing user-supplied data in a dictionary or when you write code that generates a large number of key-value pairs programatically. Let define an empty dictionary user_car and add few key-value pairs in it:

1.5. Modifying Values in a Dictionary

To modify a value in a dictionary, the methodology is the same as creating new key-value pair — i.e, give the name of the dictionary with the key in square brackets and then the new value you want associated with that key.

1.6. Removing Key-Value Pairs

Sometimes, we no longer require a piece of information that’s stored in a dictionary, in such cases, we can use the del statement to permanently remove a key-value pair. All del statement needs is the name of the dictionary and the key that we want to remove.

1.7. A Dictionary of Similar Objects

Up-till now, we are storing different kinds of information about one object, a user car We can also use a dictionary to store one kind of information about many objects as we will do in the following example

In the above example, we wrote a multi-line dictionary, in which each key-value pair is written in new line. The comma at the end of last value is not needed but a good practice.

2. LOOPING THROUGH A DICTIONARY

Several different ways exist to loop through a dictionary:

2.1. Looping Through all Key-Value Pairs

a. Example 1:

In the following example, we will print all the key-value pairs in the dictionary:

The for loop was defined in the following line:

the user_car.items(): part of the code read the entire key-value pairs.

However, you can use any keyword for the key and value, for example:

b. Example 2:

Let’s loop through a dictionary that stores one kind of information for several objects:

2.2. Looping Through all the Keys

The keys() method is useful when we don’t need to work with all of the values in a dictionary.

The for loop was defined in the following line :

whereas the fav_cars.keys(): part of the code only read key

However, we can use the following shortcut:

a. Looping with if statement

In this example, we will print custom message if the key value is Dexter:

b. Looping through keys in order

For example, we want to print the key names in alphabetic order. To do this, we will use sorted function

2.3. Looping Through All Values

We can use the .values() method to return a list of values without any keys. For example, we would like to print the favorite cars without any associated key:

a. Looping through values without repetition

In above example, ‘Tesla’ is printed twice. However, we are only interested in the unique values of car names. For this purpose, we can use the set() function

ALERT: If you have noticed that the order of printed values are not in the same order as contained in the original dictionary, this is how the Python work this out internally.

3. NESTING

Sometimes you want to store:

  • a set of dictionaries inside a list, or

  • a list of items as a value in a dictionary, or

  • a dictionary inside another dictionary … this is called Nesting

3.1. A List of Dictionaries

In the following example, we store two dictionaries inside a list:

3.2. A List in a Dictionary

We can also nest a list inside a dictionary. Let’s suppose we are storing information about blog post in a dictionary and within this dictionary, we will store the tags related to this blog post inside a list:

a. One object, multiple information

In earlier example, we stored a single value of car for each person. However, we can use a list to store more than one cars for each person:

b. Refining the code

We can see in the above example that even for person with single favorite cars, we are printing the word “… cars are:” , instead of “… car is:” Let refine the code using if statement to handle this situation:

3.3. A Dictionary in a Dictionary

Suppose our dictionary stores user information, where the key is the username and value is another dictionary including the related information about the user:

ALERT: Please note that the structure of each user dictionary is identical. However, we can make it different but the code will be more complicated.

Last updated

Was this helpful?