Get the last Key in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Get the last Key in a dictionary in Python #

Use the list() class to get the last key in a dictionary, e.g. last_key = list(my_dict)[-1]. The list class converts the dictionary to a list of keys where we can access the element at index -1 to get the last key.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } last_key = list(my_dict)[-1] print(last_key) # 👉️ country print(my_dict[last_key]) # 👉️ Austria first_key = list(my_dict)[0] print(first_key) # 👉️ id

We used the list() class to get the last key in a dictionary.

The list() class converts the dictionary to a list of keys.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } print(list(my_dict)) # 👉️ ['id', 'name', 'country']

The last step is to access the list item at index -1.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } print(list(my_dict)) # 👉️ ['id', 'name', 'country'] last_key = list(my_dict)[-1] print(last_key) # 👉️ country print(my_dict[last_key]) # 👉️ Austria first_key = list(my_dict)[0] print(first_key) # 👉️ id

As of Python 3.7, the standard dict class is guaranteed to preserve the order of keys.

Negative indices can be used to count backwards, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second-to-last item.

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.

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

To get the last key in a dictionary:

  1. Use the dict.keys() method to get a view of the dictionary's keys.
  2. Use the list() class to convert the view to a list.
  3. Access the list item at index -1 to get the last key.
main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } last_key = list(my_dict.keys())[-1] print(last_key) # 👉️ 'country' print(my_dict[last_key]) # 👉️ 'Austria'

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

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } # 👇️ dict_keys(['id', 'name', 'country']) print(my_dict.keys()) # 👇️ ['id', 'name', 'country'] print(list(my_dict.keys()))

The dict_keys object is not subscriptable, so we can't access it at a specific index.

We can convert the object to a list to get around this.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } last_key = list(my_dict.keys())[-1] print(last_key) # 👉️ 'country'
You don't have to use the my_dict.keys() method in this scenario because you can just pass the dictionary to the list() class to get a list of its keys.

However, you'll often see this approach being used to get the last key in a dictionary.

If you use a version older than Python 3.7, then the order of the keys in a dictionary is not preserved and you have to use an OrderedDict.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'Alice'), ('country', 'Austria')] ) last_key = list(my_dict)[-1] print(last_key) # 👉️ 'country'

The list() class can also be used to convert the keys of an OrderedDict to a list.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'Alice'), ('country', 'Austria')] ) print(list(my_dict)) # 👉️ ['id', 'name', 'country']

The last step is to access the key at index -1 to get the last key.

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.