Using a variable to access a dictionary Key in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Using a variable to access a dictionary Key in Python #

Use square brackets to access a dictionary key using a variable, e.g. my_dict[variable]. If the variable is an integer and the dictionary's keys are strings, convert the variable to a string when accessing a key, e.g. my_dict[str(variable)].

main.py
# 👇️ keys are strings my_dict = { '1': 'bobby', '2': 'hadz' } # 👇️ variable is an integer variable = 1 print(my_dict[str(variable)]) # 👉️ bobby # --------------------------------- my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', 'address': { 'country': 'Example' } } variable = 'site' print(my_dict[variable]) # 👉️ bobbyhadz.com variable2 = 'address' variable3 = 'country' print(my_dict[variable2][variable3]) # 👉️ Example

We used bracket notation to access a dictionary using a variable as the key.

If the dictionary's keys are strings and the variable is an integer, use the str() class to convert the variable to a string.

main.py
my_dict = { '1': 'bobby', '2': 'hadz' } variable = 1 print(my_dict[str(variable)]) # 👉️ bobby

If you don't convert the variable to a string, you'd get a KeyError.

main.py
my_dict = { '1': 'bobby', '2': 'hadz' } variable = 1 # ⛔️ KeyError: 1 print(my_dict[variable])
A KeyError exception is raised when we use square brackets to access a key that doesn't exist.

You can use the same approach to access nested dictionaries using a variable as the key.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', 'address': { 'country': 'Example' } } variable = 'site' print(my_dict[variable]) # 👉️ bobbyhadz.com variable2 = 'address' variable3 = 'country' print(my_dict[variable2][variable3]) # 👉️ Example

Alternatively, you can use the dict.get() method.

main.py
my_dict = { '1': 'bobby', '2': 'hadz' } variable = 1 print(my_dict.get(str(variable))) # 👉️ bobby # --------------------------------- my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', 'address': { 'country': 'Example' } } variable = 'site' print(my_dict.get(variable)) # 👉️ bobbyhadz.com

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } variable = 'site' print(my_dict.get(variable)) # 👉️ bobbyhadz.com variable2 = 'another' print(my_dict.get(variable2)) # 👉️ None print(my_dict.get(variable2, 'default value')) # 👉️ default value

In the last example, the specified key doesn't exist, so the provided default value is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.