Python Get First Key in Dictionary

In this Python tutorial, you will learn multiple ways to get the first key in Dictionary Python using practical examples and real-world scenarios.

When working on a Python Project, the data was stored in a dictionary, the first key storing the list of IDs. I needed to get only the first key of the dictionary to access all the IDs stored in the list, so I found multiple solutions to get the first key in Python.

3 Ways to Get First Key in Dictionary Python

Let me explain the scenario with a practical example so you will understand where you should use these approaches,

Suppose there is a dictionary of emp_data like this,

emp_data = {
    "ID":[1001, 1002, 1003, 1004, 1005, 1006],
    "Name":["John", "Jack", "Lara", "Peter", "Freddy"],
    "age":[34, 56, 33, 23, 26, 34]
}

So, there is data about the employees in the dictionary, and I need to get all the employees’ IDs, which is the first key of the dictionary.

Here, we will explain how to get the first key in the Python dictionary with the required syntax, practical examples, and realistic examples.

Python Get First Element of Dict Using list() Method

First, we will use the list() method and access the first element to get the first element of the dictionary in Python.

The list() method is a type-casting method in Python. It converts the dictionary keys into a list and accesses the first element of the list by giving its index position.

READ:  How to Create a String from a List in Python (Create a String from a List with a Separator in Python)

Syntax

list(dict_name)[0]
  • list(dict_name)[0]: Here, it converts the dict to a list using the list() method, and 0 is the index position of the list element to get the first element of the dictionary.
emp_data = {
    "ID":[1001, 1002, 1003, 1004, 1005, 1006],
    "Name":["John", "Jack", "Lara", "Peter", "Freddy"],
    "age":[34, 56, 33, 23, 26, 34]
}

first_key = list(emp_data)[0]

print("First element of the dictionary is:", first_key)
Python Get First Element of Dict Using list() Method and Slicing

In the above code, we have a dictionary called emp_data, where ID is the first key of the dict. We need to extract the first key of the dictionary in Python.

So, we use the list() method, like this: list(emp_data), and access the values by giving their index position 0, like this: list(emp_data)[0].

Get the First Key in Dictionary Python Using iter() and next() Methods

These are the generators in Python, which are used to iterate over every element one by one without using a for loop.

So, we will iterate only once to get the dictionary’s first key in Python.

Syntax

var_name = next(iter(collection))
  • next(iter(collection)): Here, the next() method is used to target the next element from the iterator in Python.
  • iter(): The iter method creates an object, which can help target one element at a time.

Let’s see how we can use both methods together to get the first element of the dictionary in Python.

state_capitals = {
    'California': 'Sacramento',
    'Arizona': 'Phoenix',
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arkansas': 'Little Rock',
}

first_state = next(iter(state_capitals))
print(f'The first state in the dictionary is {first_state}')
get first item in dictionary python using iter() and next() method

The above code has a dictionary of states and their capitals as key-value pairs. We also need to extract the first key of the dictionary in Python.

READ:  How to fix the Python 3 pickle typeerror a bytes-like object is required not 'str'

So we are using the next() and iter() methods like this, next(iter(state_capitals)).

Python Dictionary First Key Using items() Method

We can also use the items() method to get the first key of the dictionary in Python. It will return all the keys and values in the list of nested tuples like this [(key1, value1), (key2, value2)].

Also, we will use it as a for loop and break statement. So, it will stop the iteration of the for loop when it iterates for the first time using the break keyword to get the first element of the dict in Python.

Syntax

dict.items()

Let’s understand how it will work to get the first key in the dictionary Python,

state_capitals = {
    "Indiana": "Indianapolis",
    "Colorado": "Denver",
    "Mississippi": "Jackson",
    "Nevada": "Carson City",
    "Tennessee": "Nashville"
}

for state, capital in state_capitals.items():
    print(f'The first state in the dictionary is : {state}')
    break
Python Dictionary First Key Using items() Method

In the above code, we use a for loop to iterate over dict.items() like this
for state, capital in state_capitals.items():, and after printing the first element of the dictionary, it will get out of the for loop with the help of a break statement.

Conclusion

In this Python article, you learned how to Get the First Key in Dictionary Python in three different ways.

We’ve used the list() method to convert a dict to a list and access the first element by its index position. We used generators like the next() and iter() methods, and then we used the items() method to access the first element of the dict in Python.