Python Get First Key in Dictionary

In this Python tutorial, we will understand how to get first key in dictionary in Python.

There are 6 different ways to fetch or get the first key from a Python Dictionary. And all 6 methods are listed below with examples.

  • Using list() & keys() method
  • Using list() function
  • Using iter() & next() functions
  • Using dict.items() method
  • Using For Loop and dict.items()
  • Using dictionary comprehension

Get First Key in dictionary Python

Here we will see all 6 different methods to get the first key in Python Dictionary. Let us start with the first method of using list() and keys().

Method 1: Get First Key in dictionary Python using the list() & keys() method

In this method, we will first use the dict.keys() method to fetch only the keys from the given dictionary. After this, we will use the list() function to convert the keys tuple to a list.

And in the last, we will use the indexing on the list to fetch or get the first item from this list which will be the first key from the dictionary.

Here is an example of this implementation in Python.

# Create a dictionary of state capitals
state_capitals = {
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arizona': 'Phoenix',
    'Arkansas': 'Little Rock',
    'California': 'Sacramento',
}

# Get the first state and its capital from the dictionary
first_state = list(state_capitals.keys())[0]
print(f'The first state in the dictionary is {first_state}')
print(f'The capital of this state is {state_capitals[first_state]}')

In the example, we defined a dictionary of states and their capitals from the United States. Then we used the list() and dict.keys() method to fetch the first state and its capital from the dictionary.

READ:  Python find max value in a dictionary [4 Methods]

Once we execute the Python program, we will get the following result.

The first state in the dictionary is Alabama
The capital of this state is Montgomery

Read: Python Dictionary sort

Method 2: Get First Key in dictionary Python using the list() function

In the previous method, we used the list() function and dict.keys() together to fetch only keys from it.

However, if we skip using the dict.keys() and use the list() function directly on the dictionary, it will convert the dictionary to a list of keys only. And then we can use indexing on the list to fetch the first key from the given Python dictionary.

Here is an example of this task in Python.

# Create a dictionary of state capitals
state_capitals = {
    'Arizona': 'Phoenix',
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arkansas': 'Little Rock',
    'California': 'Sacramento',
}

# Get the first state and its capital from the dictionary
first_state = list(state_capitals)[0]
print(f'The first state in the dictionary is {first_state}')
print(f'The capital of this state is {state_capitals[first_state]}')

In this example, we directly converted the dictionary to a list using the list() function in Python. And then we used the [0] index to fetch the first key from the dictionary.

The result of the above program is shown below.

The first state in the dictionary is Arizona
The capital of this state is Phoenix

Read: Python Dictionary index

Method 3: Get First Key in dictionary Python using iter() & next()

In Python, the iter() is a built-in function utilized to create an iterator object from an iterable. The next() on the other hand, is used to fetch the next item from the iterator.

We can use both of these functions on the dictionary and it will return us the first key from it. Her is an example of this in Python.

# Create a dictionary of state capitals
state_capitals = {
    'California': 'Sacramento',
    'Arizona': 'Phoenix',
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arkansas': 'Little Rock',
}

# Get the first state from the dictionary
first_state = next(iter(state_capitals))
print(f'The first state in the dictionary is {first_state}')

In the above example, first, we used the iter() function on the dictionary to create an iterator object. After this, we used the next() function on the iterator object to fetch the first item from it. This first item is the first key from the dictionary.

READ:  How to convert Python tuple to dictionary

Here is the result of the program.

The first state in the dictionary is California

Read: Python Dictionary Count

Method 4: Get First Key in dictionary Python using dict.items()

The dict.items() in Python is a built-in method that takes a dictionary and returns a view object. And this view object consists of tuples of key-value pairs of the dictionary.

In this method, we will use the dict.items() method to get a tuple of key-value pairs of a dictionary. And then, we will convert that view object to a list using the list() function.

In the last, we will use indexing on the list to fetch the first key from the dictionary in Python. Here is an example of this in Python.

# Create a dictionary of state capitals
state_capitals = {
    "Colorado": "Denver",
    "Indiana": "Indianapolis",
    "Mississippi": "Jackson",
    "Nevada": "Carson City",
    "Tennessee": "Nashville"
}

# Get the first state from the dictionary
first_state = list(state_capitals.items())[0][0]
print(f'The first state in the dictionary is {first_state}')

In the above example, we utilized the dict.items() method on the state_capitals dictionary to fetch a tuple of key-value pairs. And then we used the list() function to convert it into a list.

After this, we are fetching the first element from the list using indexing.

The first state in the dictionary is Colorado

Read: Python dictionary filter

Method 5: Get First Key in dictionary Python using for loop and dict.items()

In this method, we will again use the dict.items() method to create a view object to get a tuple for each key-value pair from a dictionary. After this, we will use the for loop to iterate over the first tuple and fetch the first value from it.

READ:  How to Prepend to a List in Python [6 Methods]

Here is an example of this in Python.

# Create a dictionary of state capitals
state_capitals = {
    "Indiana": "Indianapolis",
    "Colorado": "Denver",
    "Mississippi": "Jackson",
    "Nevada": "Carson City",
    "Tennessee": "Nashville"
}

# Get the first state from the dictionary
for state, capital in state_capitals.items():
    print(f'The first state in the dictionary is {state}')
    break

In the example, we used the items() method and for loop to fetch the first key from the state_capitals dictionary in Python.

Once we execute the above Python program, we will get the following result.

The first state in the dictionary is Indiana

Read: Python Dictionary Copy

Method 6: Get First Key in dictionary Python using dictionary comprehension

In this method, we will use dictionary comprehension to fetch or get only the first key from the Python dictionary.

Here is an example of this in Python.

# Create a dictionary of state capitals
state_capitals = {
    "Indiana": "Indianapolis",
    "Colorado": "Denver",
    "Mississippi": "Jackson",
    "Nevada": "Carson City",
    "Tennessee": "Nashville"
}

# Get the first state from the dictionary
{state: state_capitals[state] for state in list(state_capitals)[:1]}
print(f'The first state in the dictionary is {state}')

In the example, we defined a dictionary named state_capitals consisting of various states from the United States and their capital. After this, we applied the dictionary comprehension on the state_capitals dictionary to fetch only the first key from it.

Here is the result of the above Python program.

Get First Key in dictionary Python
Fetching the first key from a dictionary in Python

You may also like to read the following Python tutorials.

Conclusion

So, in this Python tutorial, we understood how to get first key in dictionary in Python using 6 different methods. Moreover, we have also discussed the example related to each method in Python.

Here is the list of 6 methods that we covered.

  • Using list() & keys() method
  • Using list() function
  • Using iter() & next() functions
  • Using dict.items() method
  • Using For Loop and dict.items()
  • Using dictionary comprehension