Skip to content

Instantly share code, notes, and snippets.

@reydx
Last active October 15, 2021 02:04
Show Gist options
  • Save reydx/559ad303b9093caec1ba10b091e82830 to your computer and use it in GitHub Desktop.
Save reydx/559ad303b9093caec1ba10b091e82830 to your computer and use it in GitHub Desktop.
python skills
if key not in my_dict:
my_dict[key] = []
my_dict[key].append(new_value)
# 等价于
my_dict.setdefault(key, []).append(new_value)
# 等价于
import collections
my_dict = collections.defaultdict(list)
my_dict[key].append(new_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment