Created
June 3, 2013 11:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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