Python Forum
Help adding items to a glossary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help adding items to a glossary
#3
(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.
Reply


Messages In This Thread
Help adding items to a glossary - by lga13 - Jan-07-2019, 11:59 PM
RE: Help adding items to a glossary - by stullis - Jan-08-2019, 03:31 AM
RE: Help adding items to a glossary - by perfringo - Jan-08-2019, 06:39 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020