Dictionary
setdefaultif value does exist, return the existing value; Otherwise, set the key with a new value.
1
2
3
4
5
counts = {}
value = counts.setdefault("apples", 0)
print(value) # 0
print(counts) # {'apples': 0}
setdefault if value does exist, return the existing value; Otherwise, set the key with a new value.1
2
3
4
5
counts = {}
value = counts.setdefault("apples", 0)
print(value) # 0
print(counts) # {'apples': 0}