Behaviour of pop() for dictionaries

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Jun 14 03:29:08 EDT 2021


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]

> 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 arbitrary
value from a dict without its corresponding key. Hence the existence
of popitem().

-- 
Greg


More information about the Python-list mailing list