Skip to content

Instantly share code, notes, and snippets.

@jamesls
Created November 21, 2013 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesls/7573657 to your computer and use it in GitHub Desktop.
Save jamesls/7573657 to your computer and use it in GitHub Desktop.
OrderedDict vs. dict
from collections import OrderedDict
# This will raise a RuntimeError in python3, but will work
# in python2.
regular_dict = dict(a='a', b='c')
for i, j in regular_dict.items():
regular_dict[i + j] = j
# This will create an infinite loop and consume all memory
# in python3, but work in python2.
d = OrderedDict([('a', 'a'), ('b', 'b')])
for i, j in d.items():
d[i + j] = j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment