Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary get value without knowing the key

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?)

like image 398
Yunti Avatar asked Nov 14 '15 14:11

Yunti


People also ask

What does Python dictionary get return if key not found?

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.

What does dictionary get return if key not found?

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.


1 Answers

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.

like image 62
Delgan Avatar answered Sep 27 '22 16:09

Delgan