The pop() method exists for five mainstream data items and shows a range
of different behaviours for each of them.
But, of the five, pop for dictionaries is the only one for which the
first parameter is required and this makes d.pop() for dictionaries an
error rather than doing something useful.
I came across this in trying to use this sequence for a dictionary <d>:
if len(d.keys()) == 1:
v = d.pop()
I found it surprising that this failed given how the pops for the other
types work.
So I then tried:
v = d.values()[0]
and this doesn't work either since dict_keys items don't accept
indexing. So I was driven to use:
v = list(d.values())[0]
which seems to me a lot less intuitive (and messier) than d.pop().
These:
v = next(iter(d.values()))
v, = d.values()
also seem poor substitutes for giving d.pop() for dictionaries a useful
and intuitive purpose.
Am I missing the obvious way to obtain the value (or the key) from a
dictionary that is known to hold only one item?
More importantly, is there a good reason why we don't have d.pop() for
dictionaries?
Brian
--
https://mail.python.org/mailman/listinfo/python-list