Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Created June 3, 2013 11:35
Show Gist options
  • Save Geekfish/5697581 to your computer and use it in GitHub Desktop.
Save Geekfish/5697581 to your computer and use it in GitHub Desktop.
Use setdefault to force an empty list in case you need to iterate over existing values / keys.
my_dict = {}
my_dict['one'] = [1, 2, 3]
my_dict.setdefault('one', []).append(4)
my_dict.setdefault('two', []).append(4)
my_dict.setdefault('three', [])
# In: my_dict
# Out: {'one': [1, 2, 3, 4], 'three': [], 'two': [4]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment