I am having difficulty figuring out what the syntax would be for the last key in a Python dictionary. I know that for a Python list, one may say this to denote the last:
list[-1]
I also know that one can get a list of the keys of a dictionary as follows:
dict.keys()
However, when I attempt to use the logical following code, it doesn't work:
dict.keys(-1)
It says that keys can't take any arguments and 1 is given. If keys can't take arguments, then how can I denote that I want the last key in the list?
I am operating under the assumption that Python dictionaries are ordered in the order in which items are added to the dictionary with most recent item last. For this reason, I would like to access the last key in the dictionary.
I am now told that the dictionary keys are not in order based on when they were added. How then would I be able to choose the most recently added key?
var last = dictionary. Values. Last();
There is 2 indexing in Python that points to the last element in the list. list[ len – 1 ] : Points to last element by definition. list[-1] : In python, negative indexing starts from the end.
Delete elements of a dictionary To delete a key, value pair in a dictionary, you can use the del method. A disadvantage is that it gives KeyError if you try to delete a nonexistent key. So, instead of the del statement you can use the pop method. This method takes in the key as the parameter.
By using del keyword to remove a key from dictionary In python del keyword can be used to delete an object or item that is available in the dictionary.
It seems like you want to do that:
dict.keys()[-1]
dict.keys()
returns a list of your dictionary's keys. Once you got the list, the -1 index allows you getting the last element of a list.
Since a dictionary is unordered*, it's doesn't make sense to get the last key of your dictionary.
Perhaps you want to sort them before. It would look like that:
sorted(dict.keys())[-1]
Note:
In Python 3, the code is
list(dict)[-1]
This is no longer the case. Dictionary keys are officially ordered as of Python 3.7 (and unofficially in 3.6).
If insertion order matters, take a look at collections.OrderedDict
:
An OrderedDict is a dict that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
In [1]: from collections import OrderedDict In [2]: od = OrderedDict(zip('bar','foo')) In [3]: od Out[3]: OrderedDict([('b', 'f'), ('a', 'o'), ('r', 'o')]) In [4]: od.keys()[-1] Out[4]: 'r' In [5]: od.popitem() # also removes the last item Out[5]: ('r', 'o')
An OrderedDict is no longer necessary as dictionary keys are officially ordered in insertion order as of Python 3.7 (unofficially in 3.6).
For these recent Python versions, you can instead just use list(my_dict)[-1]
or list(my_dict.keys())[-1]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With