In Python, there are a few different ways we can get the first key/value pair of a dictionary. The easiest way is to use the items() function, convert it to a list, and access the first element.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_item = list(dictionary.items())[0]
print("The first item of the dictionary has key: " + first_item[0] + " and value: " + first_item[1])
#Output:
The first item of the dictionary has key: key1 and value: This
Another way to get the first key and value in a dictionary is to convert it to a list and access the first element. This will get the first key, and then you can get the first value by accessing that key in the
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_key = list(dictionary)[0]
print("The first key of the dictionary is: " + first_key)
#Output:
The first key of the dictionary is: key1
If you only care about getting the first value of a dictionary, you can use the dictionary values() function.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_value = list(dictionary.values())[0]
print("The first value of the dictionary is: " + first_value)
#Output:
The first value of the dictionary is: This
In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily access certain elements.
We can easily get the first item from a dictionary and get the first key and first value.
The easiest way to get the first item from a dictionary is with the items() function. The items() function returns a dict_items object, but we can convert it to a list to easily and then access the first element like we would get the first item in a list.
Below is an example of how you can use the dictionary items() function to get the first item of a dictionary.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_item = list(dictionary.items())[0]
print(first_item)
print("The first item of the dictionary has key: " + first_item[0] + " and value: " + first_item[1])
#Output:
('key1','This')
The first item of the dictionary has key: key1 and value: This
Get First Key in Dictionary Using Python
The example above is great for getting the first item in a dictionary. If you only care about getting the first key, there are a few ways you can get the first key of a dictionary.
You can use the Python dictionary keys() function, convert it to a list, and access the first element, or just convert the dictionary to a list and then access the first element.
Below shows two different ways to get the first key from a dictionary in Python.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_key = list(dictionary)[0]
print("The first key of the dictionary is: " + first_key)
first_key = list(dictionary.keys())[0]
print("The first key of the dictionary is: " + first_key)
#Output:
The first key of the dictionary is: key1
The first key of the dictionary is: key1
Get First Value in Dictionary Using Python
If you only care about getting the first value, there are a few ways you can get the first value of a dictionary.
To get the first value in a dictionary, you can use the Python dictionary values() function, convert it to a list, and access the first element.
Below shows two different ways to get the first value from a dictionary in Python.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
first_value = list(dictionary.values())[0]
print("The first value of the dictionary is: " + first_value)
#Output:
The first value of the dictionary is: This
Get the First n Items in Dictionary Using Python
We can use the items() function to get the first n items in a dictionary using Python. Since we convert the dict_items object to a list, we can use slicing just like with lists to get the first n key/value pairs in a dictionary.
Below is an example of how you can use Python to get the first 3 items in a dictionary.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
dict_items_list = list(dictionary.items())[0:3]
for x in dict_items_list:
print(x)
#Output:
('key1', 'This')
('key2', 'is')
('key3', 'a')
Get the Last Item in Dictionary Using Python
Finally, if you want to get the last item in a dictionary, or get the last key and last value, you can use a very similar method as described above.
Now, instead of getting the first element, we get the last item from the list.
Below is an example in Python of how to get the last key and last value in a dictionary.
dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }
last_item = list(dictionary.items())[-1]
print("The first item of the dictionary has key: " + last_item[0] + " and value: " + last_item[1])
#Output:
The first item of the dictionary has key: key4 and value: dictionary.
Hopefully this article has been useful for you to understand how to get the first item from a dictionary in Python.