Home
About Us Privacy Policy
 

DICTIONARIES IN PYTHON

ADVERTISEMENT

Intermediate Dictionary



What is a Dictionary, and why are they used?

In Python, a Dictionary is an in-built data type that stores values in key, value format. It is very similar to the data structure Hash Tables. But before we delve into Dictionaries, let us understand the concept of a dictionary using a real-world example.

We all have read a book in our lifetimes, and most people read from the beginning. However, when you get interrupted for something, be it a break from reading or for something else, you might want to go back to the chapter or the page you left. Many people use bookmarks or look up the book's index to resume their reading session.


Dictionary in Python is analogous to indexes in books. They are used to directly access a value based on a defined key or a reference, much like the indexes in a phone directory. I know people barely use the phone directory, but it's a good example nonetheless.

Dictionaries are a collection of unique references to mutable values.

In Python, references stored in Dictionary are ordered, mutable, and are unique.


How are they defined?

Python allows for defining of Dictionary using the curly brackets {}. They store the values in key: value pairs.

person = {
    "name": "James Bond",
    "age": 46,
}
print(person)

{'name': 'James Bond', 'age': 46}

Additionally, they can also be defined using the in-built dict() method. Below is an example:

person = dict(name="James Bond", age=46)
print(person)

{'name': 'James Bond', 'age': 46}


Advantages of using a Dictionary

If you have followed this tutorial, we have discussed the importance of data structures. If you haven't or you might want to revise, here is the link.

As discussed earlier, Dictionaries in Python allow for directly accessible values, unlike a List, Set, or a Tuple, where the values are accessed sequentially.

Let us take an example. Suppose you have a list of individuals whose information is stored according to their Birth Months. If we were to solve this conventionally, we could do something like this.

jan = ["Ajax", "Ron", "Rob", "Bob"]
feb = ["Jackson", "Sheila", "Rob"]
mar = ["Bill"]
apr = ["Tim", "Tom", "Kylie", "Kevin", "June"]
may = ["Alyssa"]
jun = ["Carlos"]
jul = ["James", "Christopher"]
aug = ["Robert", "John", "Michael"]
sep = ["Matthew", "Daniel", "Edward", "Eric", "Stephen"]
oct = ["Frank", "Douglas", "Zachary", "Gerald", "Bryan", "Albert"]
nov = ["Wayne", "Logan", "Randy", "Elijah", "Louis"]
dec = ["Johnny", "Philip"]

As you can see, the above approach is not leading to a clear, maintainable codebase. Additionally, the data values are too scattered. Since the data values logically belong to one section, having them stored as a single unit would be a much better code design.

However, doing sparsely for such a small program might still work, but it won't for more extensive programs. Ask yourself this question:

How are you going to process a million records?

To answer the above question, we immediately recognize that this approach clearly won't work. Now let us compare it with Python's dictionary implementation.

months = {
    "jan": ["Ajax", "Ron", "Rob", "Bob"],
    "feb": ["Jackson", "Sheila", "Rob"],
    "mar": ["Bill"],
    "apr": ["Tim", "Tom", "Kylie", "Kevin", "June"],
    "may": ["Alyssa"],
    "jun": ["Carlos"],
    "jul": ["James", "Christopher"],
    "aug": ["Robert", "John", "Michael"],
    "sep": ["Matthew", "Daniel", "Edward", "Eric", "Stephen"],
    "oct": ["Frank", "Douglas", "Zachary", "Gerald", "Bryan", "Albert"],
    "nov": ["Wayne", "Logan", "Randy", "Elijah", "Louis"],
    "dec": ["Johnny", "Philip"]
}

In the above example, we store all relevant values in a single data structure, i.e., a dictionary called months. Furthermore, we can access any items within it using a reference. For example, we want to access the names of people born in November; we will write the following statements:

months = {
    "jan": ["Ajax", "Ron", "Rob", "Bob"],
    "feb": ["Jackson", "Sheila", "Rob"],
    "mar": ["Bill"],
    "apr": ["Tim", "Tom", "Kylie", "Kevin", "June"],
    "may": ["Alyssa"],
    "jun": ["Carlos"],
    "jul": ["James", "Christopher"],
    "aug": ["Robert", "John", "Michael"],
    "sep": ["Matthew", "Daniel", "Edward", "Eric", "Stephen"],
    "oct": ["Frank", "Douglas", "Zachary", "Gerald", "Bryan", "Albert"],
    "nov": ["Wayne", "Logan", "Randy", "Elijah", "Louis"],
    "dec": ["Johnny", "Philip"]
}

names = months["nov"]
for name in names:
    print(name)

We will discuss accessing the values in the following sections.


ADVERTISEMENT

Find out the number of items in a Dictionary.

Since the beginning of this Python tutorial, we have been using the in-built len() method to determine the length of different data types such as List, Tuples, String. The len() method also supports the Dictionary data type and returns the number of items it stores. Below is an example:

my_info = {
    "name": "James Bond",
    "age": 46,
    "address": "123, Blakely Street",
    "cell_phone": "(007)-000-0007"
}
no_of_items = len(my_info)
print(f"The dictionary has {no_of_items} item(s).")

The dictionary has 4 item(s).


Data Types supported in Dictionary.

Dictionary in Python supports various hashable data types as keys or references. The supported data types are:

  1. int
  2. float
  3. complex
  4. tuple
  5. class instance
  6. class
  7. function

Example:

class MyFavouriteClassNameInPython:
    pass

my_fav_class = MyFavouriteClassNameInPython()

def my_favourite_function():
    pass

my_favourite_int = 8
my_favourite_float = 8.0
my_fav_complex = 9.1j
my_fav_tuple = (1, 2, 3, 4)

my_dict = {
    MyFavouriteClassNameInPython: 1,
    my_fav_class: 2,
    my_favourite_function: 3,
    my_favourite_int: 4,
    my_favourite_float: 5,
    my_fav_complex: 6,
    my_fav_tuple: 7,
}

print(my_dict)

{<class '__main__.MyFavouriteClassNameInPython'>: 1, <__main__.MyFavouriteClassNameInPython object at 0x0000023550E03FD0>: 2, <function my_favourite_function at 0x000002354F11E040>: 3, 8: 5, 9.1j: 6, (1, 2, 3, 4): 7}

Dictionary and List are Non-Hashable objects and therefore are not supported.


What is a Hashable Data type anyways?

Good question. If you have been following this Python tutorial, we discussed how values are assigned in the variables chapter. To re-iterate, a variable is a reference pointing to an object that stores the value. The variable does not hold the value itself. It is instrumental when dealing with the Dictionary data types, as it requires a fixed reference or key to produce a value.

List and Dictionary are data types whose values can be changed while maintaining the same reference. Hence, they are non-hashable and thus unsuitable candidates for references for the Dictionary data type. This information is quite helpful as most tutorials don't cover it because it seems irrelevant . However, it is crucial for deeper understanding.


How does Python handle Dictionary data type?

It is one of the most intriguing questions. I won't go into too much detail; give a brief overview. In the previous section, we discussed the Dictionary supporting only hashable data types only.

hash("name") --> 948193611238

When we set a reference or a key, its unique id gets calculated and stored. Let us take an example:

my_dict = {
    "name": "James Bond",
}

In this example, we are trying to set a key name with an associated value , James Bond.

The key name will get converted into a number much similar to like this 948193611238. Hence, whenever my_dict is accessed using the reference name, Python will calculate its hash and fetch the associated value.

Therefore, Python can support multiple hashable data types.


Accessing the values in a Dictionary

Python, being a high-level language, it provides a straightforward syntax to access values in a dictionary. The syntax to access a value is as follows:

<dict_name>[key]

Example:

my_dict = {
    1: "One",
    2: "Two",
    3: "Three",
    "name": "James Bond",
}

print(my_dict[1])
print(my_dict[2])
print(my_dict[3])
print(my_dict["name"])

One
Two
Three
James Bond


Extracting all the keys in a Dictionary, and why is it useful?

We have discussed accessing Dictionary values, but what about extracting all the keys in a Dictionary and its applicability. Let us take a real-world example to help understand the importance of this helpful feature.

Assume you are creating a program to store all the languages spoken in countries. Here is what the Python representation might look like:

# For simplicity we will discuss only a few countries.
countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

Now suppose, you might want to find out all the countries supported. Python allows you to extract the keys in a dictionary.

countries_supported = countries.keys()
print(countries_supported, type(countries_supported))

dict_keys(['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy']) <class 'dict_keys'>

This valuable feature of Python's Dictionary allows for storing both the reference and the value together in a cohesive data structure that provides for logically related data to be together.


Converting of class dict_keys to List or Tuple

Python allows for the conversion of keys returned by the in-built keys() method using the list() or tuple() method to convert. Below is an example:

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

countries_supported_list = list(countries.keys())
countries_supported_tuple = tuple(countries.keys())

print(countries_supported_list, type(countries_supported_list))
print(countries_supported_tuple, type(countries_supported_tuple))

['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy'] <class 'list'> ('usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy') <class 'tuple'>


Extracting values from the Dictionary.

Similar to the keys() method discussed in the prior section, Python allows for extracting the Dictionary values. For that, Python provides the conveniently named method values(). Below is an example:

countries = {
        "usa": ("english"),
        "canada": ("english", "french"),
        "brazil": ("spanish"),
        "france": ("french"),
        "china": ("mandarin chinese"),
        "australia": ("english"),
        "russia": ("russian"),
        "spain": ("spanish"),
        "japan":("japanese"),
        "italy": ("italian"),
}

languages_spoken = countries.values()
for lang in languages_spoken:
    print(lang)

english
('english', 'french')
spanish
french
mandarin chinese
english
russian
spanish
japanese
italian


ADVERTISEMENT

Finding the presence of a key in a Dictionary.

We have discussed extracting keys and values from a Dictionary. What if we want to check the presence of the key before we try to access its value. To help illustrate this scenario, let us solve this problem the conventional way:

Suppose we want to find whether "usa" is present in the Dictionary or not.

# Finding the presence of the key, the conventional way.
countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

all_countries = countries.keys()
for country in all_countries:
    if country == "usa":
        print("USA is present")

USA is present

As you can observe, we had to traverse through the entire country list to verify its presence, and doing so results in a larger code base and opens opportunities for bugs to seep through.

Python, a high-level programming language, provides the in operator as discussed in the operator's chapter.

Below is the same example using the in operator:

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

if "usa" in countries:
    print("USA is present")

USA is present

The above example is a much neater, understandable, and more manageable codebase.


Looping through the Dictionary

During development, you might encounter scenarios where you want to display the key and the associated value. Here is how you would loop through the Dictionary to print the same.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

for country in countries:
    print(f"{country} : {countries[country]}")

usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian

Let us break down the above statement for clarity.

#1:

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

This statement is declaring a dictionary named countries with the languages spoken in each.

#2:

for country in countries:

This statement fetches the key from the Dictionary and stores it in a temporary variable named country.

#3:

print(f"{country} : {countries[country]}")

This statement accesses the value in the Dictionary using the fetched key.


However, Python provides an alternate way to access both the key and value simultaneously using the in-built items() method. Below is an example:

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

for key, value in countries.items():
    print(f"{key} : {value}")

usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian

The in-built items() method returns both the associated key and its value in a tuple.


Nested Dictionary

Python being a high-level language allows for having a Dictionary with a Dictionary. Here is an example:

# This dictionary stores population of 3 cities within 3 states.
population = {
    "california": {
        "los angeles": 3979576,
        "san diego": 1423851,
        "san jose": 1021795
    },
    "washington": {
        "seattle": 724305,
        "spokane": 217353,
        "tacoma": 212869,
    },
    "texas": {
        "houston": 2310432,
        "san antonio": 1508083,
        "dallas": 1330612
    }
}

print(population["california"]['los angeles'])
print(population["washington"]["tacoma"])
print(population["texas"]["dallas"])

3979576
212869
1330612

Having nested Dictionaries help with accessing data managed by categories.

In the above example, we categorized cities under their respective states and then access their population details.

The nested Dictionary helps store and access categorized data.


ADVERTISEMENT

Methods

clear()

The in-built clear() method clears the entire contents of the Dictionary.

my_dict = {
    1: "One",
    2: "Two",
    3: "Three",
}
print(my_dict)

# Clearing the dictionary
my_dict.clear()
print(my_dict)

{1: 'One', 2: 'Two', 3: 'Three'}
{}


copy()

We have discussed this earlier as well; there are referenced-based and value-based data types. In Python, Dictionaries are referenced based. Hence, simply assigning the Dictionary to a new variable won't create a new Dictionary; it will refer to the same Dictionary. Below is an example:

my_dict = {
    1: "One",
    2: "Two",
    3: "Three",
}

# This statement will assign a reference
x = my_dict

# Change the value
x[1] = "Four"
print(my_dict)

{1: 'Four', 2: 'Two', 3: 'Three'}

As you can observe from the output, my_dict is changed, although we did not directly change it. It is because the variable x is now referring to my_dict. Hence, any change made in any variable, either x or my_dict, will reflect when accessing the Dictionary using any reference.

To create a 'deep' copy of the Dictionary, Python provides the in-built copy() method.

# Original dictionary
my_dict = {
    1: "One",
    2: "Two",
    3: "Three",
}

# Create a separate copy
x = my_dict.copy()

x[1] = "Four"
print(my_dict)
print(x)

{1: 'One', 2: 'Two', 3: 'Three'}
{1: 'Four', 2: 'Two', 3: 'Three'}


fromkeys()

This in-built fromkeys() method will create a dictionary with a specified key and value. This method is helpful when you might want a Dictionary having the same default values for initialization.

keys = ("name", "age", "address")
values = ""
my_dict = dict.fromkeys(keys, values)
print(my_dict)

{'name': '', 'age': '', 'address': ''}

The values argument in fromkeys() method is optional and defaults to None.


get()

This in-built get() method in Python will return the value from the Dictionary. If the key is absent, None is returned.

my_dict = { "name": "James Bond", "age": 46 }
print(my_dict.get("name"))

James Bond


items()

These in-built items() in Python will return a tuple containing both the key and values.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

for key, value in countries.items():
    print(f"{key} : {value}")

usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian


keys()

This in-built keys() method in Python will return all the keys in the Dictionary wrapped in dict_keys object. However, you can convert it into a list or a tuple using the list() and tuple() methods, respectively.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

countries_supported = countries.keys()
print(countries_supported)

dict_keys(['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy'])


pop()

This in-built pop() method in Python allows for the deletion of an item with the specified key.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

print("italy" in countries)
countries.pop("italy")
print("italy" in countries)

True
False

Alternatively, you can also use the del keyword to delete items.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

print("italy" in countries)
del countries["italy"]
print("italy" in countries)

True
False


popitem()

This in-built popitem() method in Python will remove the last inserted item from the Dictionary.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

countries["andorra"] = ("english")
print("andorra" in countries)
countries.popitem()
print("andorra" in countries)

True
False


setdefault()

The in-built setdefault() method in Python will return the value of the specified key. If the queried key does not exist, it will insert the key and return its value.

my_car = { "name": "Ford", "model": "2018", "color": "blue" }
x = my_car.setdefault("make", "bronco")
print(x)

bronco


update()

The in-built update() method in Python will update the Dictionary with the specified key-value pairs.

my_car = { "name": "Ford", "model": "2018", "color": "blue" }
my_car.update({"model": "bronco"})
print(my_car)

{'name': 'Ford', 'model': 'bronco', 'color': 'blue'}


values()

The in-built values() method in Python will return the values of all keys in the Dictionary.

countries = {
    "usa": ("english"),
    "canada": ("english", "french"),
    "brazil": ("spanish"),
    "france": ("french"),
    "china": ("mandarin chinese"),
    "australia": ("english"),
    "russia": ("russian"),
    "spain": ("spanish"),
    "japan":("japanese"),
    "italy": ("italian"),
}

all_languages = countries.values()
print(all_languages)

dict_values(['english', ('english', 'french'), 'spanish', 'french', 'mandarin chinese', 'english', 'russian', 'spanish', 'japanese', 'italian'])


Conclusion

In this chapter, we explained the concept of Dictionaries, their usage, their definition, how to access their values, categorization of data, and its appropriate methods.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.