Get dictionary Key as a Variable in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Get dictionary Key as a Variable in Python #

To get a dictionary key as a variable:

  1. Use a for loop to iterate over the dictionary's items.
  2. Assign the key to a variable.
  3. The variable will store the key of the current iteration.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } # ✅ get dictionary keys and values as variables for key in my_dict: # first_name Bobby # last_name Hadz # site bobbyhadz.com print(key, my_dict[key]) for key, value in my_dict.items(): # first_name Bobby # last_name Hadz # site bobbyhadz.com print(key, value) # ✅ get specific dictionary key as variable first_key = list(my_dict)[0] print(first_key) # 👉️ first_name # ✅ get specific dictionary value as variable first_value = list(my_dict.values())[0] print(first_value) # 👉️ Bobby

The first example uses a for loop to iterate directly over the dictionary.

The key variable stores the key of the current iteration.

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

You can use square brackets if you need to access the corresponding value.

You can also use the dict.items() method to get the dictionary's keys and values as variables.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } for key, value in my_dict.items(): # first_name Bobby # last_name Hadz # site bobbyhadz.com print(key, value)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

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

On each iteration, the key variable stores the key of the current iteration and the value variable stores the corresponding value.

If you need to store a specific dictionary key in a variable, convert the dictionary to a list of keys and access the key at its index.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } first_key = list(my_dict)[0] print(first_key) # 👉️ first_name first_value = list(my_dict.values())[0] print(first_value) # 👉️ 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.

We used the list() class to convert the dictionary to a list of keys.

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

We could have also used the dict.keys() method to be more explicit.

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

If you need to store a specific dictionary value in a variable, convert the dictionary's values to a list and access the value at its index.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } first_value = list(my_dict.values())[0] print(first_value) # 👉️ Bobby

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())

View objects are not subscriptable (cannot be accessed at an index), so we had to convert the view object to a list before accessing a specific 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.