python 3 dict: .keys(), .values(), and .item()
ethan at stoneleaf.us
Sat Jan 7 18:34:16 EST 2017
On 01/07/2017 03:04 PM, Peter Otten wrote:
> Ethan Furman wrote:
>> In Python 2 we have:
>> dict().keys() \
>> dict().items() --> separate list() of the results
>> dict().values() /
>> dict().iter_keys() \
>> dict().iter_items() --> integrated iter() of the results
>> dict().iter_values() /
> I guess you didn't use these as often as I did ;)
> By the way, there are also the virtually unused/unknown dict.viewXXX()
> methods that are the exact? equivalent to the dict.XXX() methods in Python
Ah, right -- in fact, those are the ones that got transplanted, I think.
>> In other words, what used to be a completely safe operation now is not.
> Is code that was written for Python 3 riddled with list(...) calls?
Generally, or just around dicts? I know I've placed list() around a few dict
calls (when I am going to modify the dict in the loop) or I make a separate list
to save the modifications and the bulk change in a separate loop.
> Do you see
> RuntimeError: dictionary changed size during iteration
No, thank goodness. Still embarrassing when it happens, though. :(
Thinking on it some more, I'm pretty sure I would have ended up with another
exception of a different flavor, so overall no extra harm done. ;)
More information about the Python-list