Skip to content

Instantly share code, notes, and snippets.

@qqpann
Created September 2, 2018 11:19
Show Gist options
  • Save qqpann/e5f7bdb774ecb175e205609e23f63e25 to your computer and use it in GitHub Desktop.
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
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