Open In App

Python | Get the first key in dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

Many times, while working with Python, we can have a situation in which we require to get the initial key of the dictionary. There can be many specific uses for it, either for checking the indexing or many more of these kinds. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list() + keys()

The combination of the above methods can be used to perform this particular task. In this, we just convert the entire dictionaries’ keys extracted by keys() into a list and just access the first key. Just one thing you have to keep in mind while using this i.e its complexity. It will first convert the whole dictionary to list by iterating over each item and then extracting its first element. Using this method complexity would be O(n). 

Python3




# Python3 code to demonstrate working of
# Getting first key in dictionary
# Using keys() + list()
 
# initializing dictionary
test_dict = {'Gfg': 1, 'is': 2, 'best': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using keys() + list()
# Getting first key in dictionary
res = list(test_dict.keys())[0]
 
# printing initial key
print("The first key of dictionary is : " + str(res))


Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #2 : Using next() + iter()

 This task can also be performed using these functions. In this, we just take the first next key using next() and iter function is used to get the iterable conversion of dictionary items. So if you want only the first key then this method is more efficient. Its complexity would be O(1). 

Python3




# Python3 code to demonstrate working of
# Getting first key in dictionary
# Using next() + iter()
 
# initializing dictionary
test_dict = {'Gfg': 1, 'is': 2, 'best': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using next() + iter()
# Getting first key in dictionary
res = next(iter(test_dict))
 
# printing initial key
print("The first key of dictionary is : " + str(res))


Method #3: Using For Loop : This can be done using for loop. In this method run a for loop get first key and break the loop and print first key of the dictionary.

Python3




# create dict
student_name = {'sham': 10, 'nil': 30, 'veer': 40, 'ram': 50}
 
# for loop for getting first key
for new_s, new_val in student_name.items():
    # print first key
    print(new_s)
# after getting first key break loop
    break


Output

sham

Time complexity: O(1), as the loop iterates only once and the break statement exits the loop after the first iteration.
Auxiliary space: O(1), as the amount of memory used by the program remains constant regardless of the size of the input data. 

Method #4: Using dictionary indexing

  1. Retrieve the first key from the dictionary by indexing it with [0].
  2. Print the first key using the print() function.

Python3




# create dict
student_name = {'sham': 10, 'nil': 30, 'veer': 40, 'ram': 50}
 
# get first key using dictionary indexing
first_key = list(student_name.keys())[0]
 
# print first key
print(first_key)


Output

sham

Time complexity: O(N)

Creating the dictionary takes O(n) time, where n is the number of key-value pairs in the dictionary.
Getting the first key using dictionary indexing takes O(1) time.
Converting the keys to a list takes O(n) time.
Indexing the first key from the list takes O(1) time.
Therefore, the total time complexity is O(n).

Auxiliary space: O(N)

The space required for creating the dictionary is O(n).
The space required for storing the list of keys is also O(n).
Therefore, the total auxiliary space complexity is O(n).



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads