Get the first Key in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Get the first Key in a dictionary in Python #

To get the first key in a dictionary:

  1. Use the iter() function to get an iterator of the dictionary's keys.
  2. Use the next() function to get the first key in the dictionary.
  3. For example, first_key = next(iter(my_dict)).
main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } # ✅ get first key in dictionary using iterator first_key = next(iter(my_dict)) print(first_key) # 👉️ 'id' first_key_value = next(iter(my_dict.items())) print(first_key_value) # 👉️ ('id', 1) print(my_dict[first_key]) # 👉️ 1 # ------------------------------------------------- # ✅ get first key in dictionary using list() class first_key = list(my_dict)[0] print(first_key) # 👉️ 'id'

We used the iter() function to get an iterator object of the dictionary's keys.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } # ✅ get first key in dictionary using iterator # 👇️ <dict_keyiterator object at 0x7f2a474e7bf0> print(iter(my_dict)) print(next(iter(my_dict))) # 👉️ 'id'

The next function returns the next item from the iterator.

You can provide a default value that gets returned if the dictionary is empty.

main.py
my_dict = {} print(iter(my_dict)) print(next(iter(my_dict), 'default')) # 👉️ 'default'

If the dictionary is empty and no default is provided, a StopIteration exception is raised.

If you need to get the first key-value pair in a dictionary, use the dict.items() method.

main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } first_key = next(iter(my_dict)) print(first_key) # 👉️ 'id' first_key_value = next(iter(my_dict.items())) print(first_key_value) # 👉️ ('id', 1) print(first_key_value[0]) # 👉️ 'id' print(first_key_value[1]) # 👉️ 1

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

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

This approach is quite efficient when you only need to get the first key in a dictionary.

Alternatively, you can use the list() class.

Get the first Key in a dictionary using list() #

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

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

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

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 0.

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

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

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 first 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 0 to get the first key.
main.py
my_dict = { 'id': 1, 'name': 'Alice', 'country': 'Austria' } first_key = list(my_dict.keys())[0] print(first_key) # 👉️ 'id' print(my_dict[first_key]) # 👉️ 1

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' } first_key = list(my_dict.keys())[0] print(first_key) # 👉️ 'id' print(my_dict[first_key]) # 👉️ 1
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 first 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')] ) first_key = list(my_dict)[0] print(first_key) # 👉️ 'id' print(my_dict[first_key]) # 👉️ 1

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 0 to get the first 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.