Skip to content

Instantly share code, notes, and snippets.

@simonvc
Created October 30, 2013 10:57
Show Gist options
  • Select an option

  • Save simonvc/7230732 to your computer and use it in GitHub Desktop.

Select an option

Save simonvc/7230732 to your computer and use it in GitHub Desktop.
python dictionary has a list or get an empty (but iterable) list
>>> for x in a.get('a list'):
... print x
...
1
2
3
4
5
6
>>> for x in a.get('b list'):
... print x
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
>>> for x in (a.get('b list') or []):
... print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment