On 14/06/2021 08:29, Greg Ewing wrote:
On 14/06/21 4:19 am, BlindAnagram wrote:
Am I missing the obvious way to obtain the value (or the key) from a dictionary that is known to hold only one item?

v = d.popitem()[1]

Thanks, Greg, I missed that.

More importantly, is there a good reason why we don't have d.pop() for dictionaries?

My guess is because it's not generally useful to get an returns the value 
arbitrary
value from a dict without its corresponding key. Hence the existence
of popitem().

However, d.pop(key, [default]) returns the value (or the default) and consistency with other pops (a good thing in my view) would suggest that d.pop() could return a random value, which would serve my purpose when there is only one element.


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to