Jan-08-2019, 06:39 AM
(Jan-08-2019, 03:31 AM)stullis Wrote: I would use dict.keys() instead of list(dict). Probably just a style difference.
In this specific example it's style difference. However, there is more to that. You can't iterate over dict.keys() and modify the dictionary at the same time, but with list(dict) you can:
>>> d = {k: k for k in range(4)} >>> d {0: 0, 1: 1, 2: 2, 3: 3} >>> for k in d.keys(): ... del d[k] ... Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: dictionary changed size during iteration >>> d {1: 1, 2: 2, 3: 3} # deleted first key and raised RuntimeError >>> for k in list(d): ... del d[k] ... >>> d {} # deleted all keys without error
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.