Created
September 2, 2018 11:19
-
-
Save qqpann/e5f7bdb774ecb175e205609e23f63e25 to your computer and use it in GitHub Desktop.
[Python list as dict value] Best practice to use list as dict value #python #dict #list
This file contains hidden or 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
dic = dict() | |
key, value = 'some key', 'some value' | |
dic[key] = dic.get(key, []) + [value] | |
dic.setdefault(key,[]).append(value) | |
# doesn't work: | |
# dic[key] = dic.get(key, []).append(value) | |
# https://docs.python.org/3/library/stdtypes.html#dict.setdefault |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment