How to avoid adding duplicate keys to dictionary by mistake ...

There would be cases where large dictionaries are being embedded in the code (multiple pages in length) and users might append something to end of the dictionary, just assuming that its not already in the dictionary. 

This could be easily avoided by following dict() instead of {} notation for dictionaries.

  >>> a = {
  ...   'a': 1,
  ...   'b': 2,
  ...   'c': 3,
  ...   'd': 'nextpage',
  ...   'a': 0  ... }
  >>> print a
  {'a': 0, 'c': 3, 'b': 2, 'd': 'nextpage'}
  >>> a = dict(
  ...   a=1,
  ...   b=2,
  ...   c=3,
  ...   d='nextpage',
  ...   a=0
  ... )
    File "<stdin>", line 6  SyntaxError: keyword argument repeated

Comments

Popular posts from this blog

Multiple repeat error when the regex has multiple wildcards in python re module

Weakref proxy is for instance only ...

To speed up accessing files on nfs shares on a ubuntu machine ...