Assign a dictionary value to a Variable in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Assign a dictionary value to a Variable in Python #

Use bracket notation to assign a dictionary value to a variable, e.g. first = my_dict['first_name']. The left-hand side of the assignment is the variable's name and the right-hand side is the value.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } first = my_dict['first_name'] print(first) # 👉️ Bobby first = list(my_dict.values())[0] print(first) # 👉️ Bobby key = 'last_name' value = my_dict[key] print(value) # 👉️ Hadz

The first example uses square brackets to access a dictionary key and assigns the corresponding value to a variable.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } first = my_dict['first_name'] print(first) # 👉️ Bobby

If you need to access the dictionary value using an index, use the dict.values() method.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } first = list(my_dict.values())[0] print(first) # 👉️ Bobby
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } # 👇️ dict_values(['Bobby', 'Hadz', 'bobbyhadz.com']) print(my_dict.values())

We had to use the list() class to convert the view object to a list because view objects are not subscriptable (cannot be accessed at an index).

You can use the same approach if you have the key stored in a variable.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } key = 'last_name' value = my_dict[key] print(value) # 👉️ Hadz

If you try to access a dictionary key that doesn't exist using square brackets, you'd get a KeyError.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com', } # ⛔️ KeyError: 'first' first = my_dict['first']

On the other hand, the dict.get() method returns None for non-existent keys by default.

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

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', } print(my_dict.get('site')) # 👉️ bobbyhadz.com print(my_dict.get('another')) # 👉️ None print(my_dict.get('another', 'default value')) # 👉️ default value
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.