Last updated: Apr 9, 2024
Reading timeยท4 min
Use the list()
class to get the last key in a dictionary, e.g.
list(my_dict)[-1]
.
The list
class converts the dictionary to a list of keys where we can access
the element at index -1
to get the last key.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_key = list(my_dict)[-1] print(last_key) # ๐๏ธ country print(my_dict[last_key]) # ๐๏ธ Belgium first_key = list(my_dict)[0] print(first_key) # ๐๏ธ id
If you only need the last value, use the dict.values()
method instead.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_value = list(my_dict.values())[-1] print(last_value) # ๐๏ธ Belgium print(list(my_dict.values())) # ๐๏ธ [1, 'Bobby Hadz', 'Belgium']
We used the list() class to get the last key in a dictionary.
The list()
class converts the dictionary to a list of keys.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } print(list(my_dict)) # ๐๏ธ ['id', 'name', 'country']
The last step is to access the list item at index -1
.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } print(list(my_dict)) # ๐๏ธ ['id', 'name', 'country'] last_key = list(my_dict)[-1] print(last_key) # ๐๏ธ country last_value = my_dict[last_key] print(last_value) # ๐๏ธ Belgium
As of Python 3.7, the standard dict
class is guaranteed to preserve the order
of keys.
Negative indices can be used to count backward, e.g. my_list[-1]
returns the
last item in the list and my_list[-2]
returns the second-to-last item.
0
, and the last item has an index of -1
or len(my_list) - 1
.You can also use the dict.keys()
method explicitly to get the last key in a
dictionary.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_key = list(my_dict.keys())[-1] print(last_key) # ๐๏ธ 'country' print(my_dict[last_key]) # ๐๏ธ 'Belgium'
If you only need the last value, use the dict.values()
method instead.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_value = list(my_dict.values())[-1] print(last_value) # ๐๏ธ Belgium
The dict.keys() method returns a new view of the dictionary's keys.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } # ๐๏ธ dict_keys(['id', 'name', 'country']) print(my_dict.keys()) # ๐๏ธ ['id', 'name', 'country'] print(list(my_dict.keys()))
The dict_keys
object is not subscriptable, so we can't access it at a specific
index.
We can convert the object to a list to get around this.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_key = list(my_dict.keys())[-1] print(last_key) # ๐๏ธ 'country'
my_dict.keys()
method in this scenario because you can just pass the dictionary to the list()
class to get a list of its keys.However, you'll often see this approach being used to get the last key in a dictionary.
Alternatively, you can use the reversed()
function:
next()
function to get the first key from the reversed iterator.my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_key = next(reversed(my_dict.keys()), None) print(last_key) # ๐๏ธ country last_value = my_dict[last_key] print(last_value) # ๐๏ธ Belgium
If you only need the last value, use the dict.values()
method.
my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'country': 'Belgium' } last_value = next(reversed(my_dict.values()), None) print(last_value) # ๐๏ธ Belgium
The reversed() function takes an iterator, reverses it and returns the result.
result = list(reversed(range(1, 11))) print(result) # ๐๏ธ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The next() function returns the next item from the provided iterator.
The function can be passed a default value as the second argument.
If the iterator is exhausted or empty, the default value is returned.
We used None
as the default value, so if the dictionary is empty, None
is
returned.
my_dict = {} last_key = next(reversed(my_dict.keys()), None) print(last_key) # ๐๏ธ None if last_key is not None: print(my_dict[last_key])
If the iterator is exhausted or empty and no default value is provided, a
StopIteration
exception is raised.
If you use a version older than Python 3.7, then the order of the keys in a
dictionary is not preserved and you have to use an OrderedDict
.
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'Bobby Hadz'), ('country', 'Belgium')] ) last_key = list(my_dict)[-1] print(last_key) # ๐๏ธ 'country'
The list()
class can also be used to convert the keys of an OrderedDict
to a
list.
from collections import OrderedDict my_dict = OrderedDict( [('id', 1), ('name', 'Bobby Hadz'), ('country', 'Belgium')] ) print(list(my_dict)) # ๐๏ธ ['id', 'name', 'country']
The last step is to access the key at index -1
to get the last key.
You can learn more about the related topics by checking out the following tutorials: