In this Python tutorial, we will discuss the How to get a first key in a dictionary in Python. Here we will also cover the below examples:
- Get First key in dictionary Python
- Get value of first key in dict Python
- Get key of first element in dictionary Python
- Python get key of first item in dictionary
- Get first key value pair in dictionary Python
Get First Key in dictonary Python
- In this program, we will discuss how to get the first key in a Python dictionary.
- Here we will discuss how to fetch the first key of a dictionary. In Python, the dictionary contains elements in the form of key-value pairs.
- There are various method to perform this particular task
- By using keys() method
- By using next()+iter() method
- By using forloop method
- By using N keys method
Let us understand and check how to get the first key from a dictionary by using the keys() method
Python provides function keys() that always return an iterable sequence of all given keys which is available in the dictionary and this method does not take any parameter.
Syntax:
Here is the Syntax of dict.keys() method
dict.keys()
Note: This function does not pass any argument and always returns an object that displays all the keys.
Source Code:
my_new_dict ={'George':192,'Micheal':269,'James':159,'Potter':432}
new_k = list(my_new_dict.keys())[0]
print("Get first key from dictionary:",new_k)
In the above code first, we have created a dictionary ‘my_new_dict’ that contains key-value pair elements. Now we have to use dict.keys() method for converting the sequence of keys to a list. Once you will print ‘new_k’ then the output will display the first key element from the dictionary.
Here is the execution of the following given code
An alternative approach to getting the first key from a dictionary by using the next()+iter() method
In Python, the next() and iter() method is used to get the iterable sequence of dictionary elements. The iterator object is created by using iter() and the next() method is used to return the next value from an object like a dictionary. When we reach the last element and there is no more data to be returned then it will display ‘stopiteration’.
Example:
Let’s take an example and understand how to get the first key in a Python dictionary
employee_dictionary = {'Oliva' : 145, 'William' : 389, 'Elijah' : 921}
new_output = next(iter(employee_dictionary))
print(new_output)
Here is the output of the following given code
By using for loop method
This is another method to get the initial key from a dictionary by using for loop method. In Python for loop runs a block of code and in this example when we will get the initial key the loop will break and display the result.
Source Code:
fruit_name = { 'Apple': 48, 'Oranges': 289, 'Banana': 192, 'Cherry': 158}
for new_k, new_val in fruit_name.items():
print(new_k)
break
In this program after getting the initial key of the dictionary stops the loop by using a break statement. Once you will print ‘new_k’ then the output will display the first key ‘Apple’.
You can refer to the below Screenshot for getting the first key from the dictionary
By using N keys method
In this program first, we will select the ‘i’ number of keys from a dictionary and then convert the keys of a dictionary to a list by using the list() method. In the below example we have selected the first I entry that is ‘i=1’.
Source Code:
Country_name = { 'Germany': 378, 'Australia': 167, 'Newyork': 556, 'China': 443}
i = 1
new_ini_ke = list(Country_name.keys())[:i]
print("Initial key from dictionary:",new_ini_ke)
In the above program first, we have initialized a dictionary that contains key-value pairs. Once you will print ‘new_ini_ke’ then the output will display ‘Germany’.
Here is the Screenshot of the following given code
Read: Python dictionary increment value
Get value of first key in dict Python
- Let us see how to get the value of the first key from the Python dictionary.
- To fetch the first value from a dictionary there are various methods and keywords to solve this problem.
- By using dict.values() method
- By using item() method
- By using N values method
Let us understand and check how to get the value of the initial key from a dictionary by using the dict.values() method.
In Python, the dictionary provides a dict.values() method and returns an object that displays a list that contains all the values from a dictionary. In this method, if the dictionary has no values then it will return an empty dictionary.
Syntax:
Here is the Syntax of the dict.values() method
dict.values()
Note: This function does not take any argument and returns dictionary values in the form of a list.
Example:
Country_name = { 'Newzealand': 490, 'Switerland': 380, 'Russia': 117, 'France': 190}
new_val = list(Country_name.values())[0]
print('Get value from first key:', new_val)
In the above Program first, we have created a dictionary ‘Country_name’ that contains key-value pair elements. Now we have to use dict.values() method for converting the sequence of values to a list. Once you will print ‘new_val’ then the output will display the initial key value from the dictionary.
Here is the implementation of the following given code
By using item() method
- This is another method to check how to get the initial key value from a dictionary.
- To perform this particular task we can apply the concept of dict.items() method. In Python, this method is used to return an object that displays a list with all dictionary keys with values.
Syntax:
Here is the Syntax of the dict.items() method
dict.items()
Note: This function does not take any argument and always returns an object.
Source Code:
Flower_name = { 'Rose': 623, 'Tulip': 168, 'Lilly': 251, 'Jasmine': 390}
get_val = list(Flower_name.items())[0][1]
print("Initial val from dictionary:", get_val)
In the above program, we have initialized a dictionary and then create a variable ‘get_val’. Now we have to use the list() method and assign a key-value pair index [0][1].
You can refer to the below Screenshot
By using N values method
In this example first, we will select the ‘m’ number of key-value pairs from a dictionary and then convert the values of a dictionary to a list by using the list() method.
Source Code:
Student_name = { 'George': 45, 'Aris': 23, 'Elijah': 69, 'William': 109}
m = 1
new_get_val = list(Student_name.values())[:m]
print("Get selected value from dictionary:",new_get_val)
Here is the Screenshot of the following given code
This is how to get the value of the initial key from a dictionary.
Read: Python dictionary of lists
Get key of first element in dictionary Python
- Here we can see how to get the first key element from the Python dictionary.
- By using the next() and iter() method we can perform this particular task and these methods are used to get the iterable sequence.
Source Code:
Country_name = { 'Bangladesh': 647, 'Japan': 810, 'Turkey': 942, 'China': 280}
new_result = next(iter(Country_name))
print("Initial first key element:", new_result)
In the above example first, we have created a dictionary and then use the next()+iter() method to get the first key element from the dictionary.
Here is the Output of the following given code
Read: Python dictionary extend
Python get key of first item in dictionary
- In this Program, we will discuss how to get the first key item from a dictionary.
- By using the list comprehension method we can solve this task and this constructor is used to create a new from another iterable sequence like a dictionary. In this example, if the given value does not exist in the dictionary then it will return an empty array.
Source Code:
new_dictionary= { 'Chinese': '256', 'Italian': '180', 'Mexican': 632, 'Thai': 189}
new_output = [new_key for new_key, new_val in new_dictionary.items() if new_val == '256']
print(new_output)
In the above code, we apply the concept of the list comprehension method [] to iterate keys and values of the dictionary. In this method, it will check the condition if new_val ==’256′ exists in a dictionary or not. If it is available then it will display the first key element.
You can refer to the below Screenshot
Read: Python Dictionary Copy
Get first key value pair in dictionary Python
- Here we can see how to get a first key-value element from a Python dictionary.
- By using iter() and next() method we can iterate all key-value pairs of the dictionary. In Python the iter() function creates a iterable object and it can iterated one element at a time. To get the first element of this sequence we can use the next() function in which we have passed iter() and items() function as a parameter.
Source Code:
new_my_dict= { 'b': 167, 'z': 119, 'p': 108, 'w': 167}
initial_ele = next(iter((new_my_dict.items())) )
print( initial_ele[0])
print(initial_ele[1])
print("Initial key-value pair element:",initial_ele)
Here is the execution of the following given code
An alternative approach to check how to get the first key-value pair from Python dictionary
By using the list and slicing method we have selected the ‘m’ number of entries from the list[:m]. In this example, we have selected the first key-value pairs from a dictionary.
Source Code:
student_name= { 'Chris': 541, 'Hemsworth': 930, 'James': 162, 'Oliva': 108}
m = 1
result= list(student_name.items())[:m]
for new_k,new_val in result:
print("Get key-value pair from dictionary:",new_k, ':', new_val)
You can refer to the below Screenshot
You may like to read some of our tutorials on Python.
- Python dictionary multiple keys
- Python Dictionary to CSV
- Python find number in String
- Case statement in Python
- If not condition in python
- Reverse a list in python
- Python dictionary of tuples
- Python dictionary pop
- Python convert DataFrame to list
- Get all values from a dictionary Python
In this Python tutorial, we have discussed How to get a First Key in a dictionary in Python. Here we have also covered the below examples:
- Get First key in dictionary Python
- Get value of first key in dict Python
- Get key of first element in dictionary Python
- Python get key of first item in dictionary
- Get first key value pair in dictionary Python
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.