In python if I have a dictionary which has a single key value pair and if I don't know what the key might be, how can I get the value?
(and if I have a dict with more than 1 key, value pair, how can I return any one of the values without knowing any of the keys?)
get() method returns: the value for the specified key if key is in the dictionary. None if the key is not found and value is not specified.
If given key does not exists in dictionary, then it returns the passed default value argument. If given key does not exists in dictionary and Default value is also not provided, then it returns None.
You just have to use dict.values()
.
This will return a list containing all the values of your dictionary, without having to specify any key.
You may also be interested in:
.keys()
: return a list containing the keys.items()
: return a list of tuples (key, value)
Note that in Python 3, returned value is not actually proper list but view object.
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