
from collections import defaultdict
int_dict = defaultdict(int)
print(int_dict['key'])
>> 0
print(int_dict)
>> defaultdict(<class 'int'>, {'key': 0})
list, set, int 등으로 지정할 수 있다! 👀amazing..from collections import defaultdict
list_dict = defaultdict(list)
print(list_dict['key'])
>> []
print(list_dict)
>> {'key': []}
value값이 빈 리스트다.from collections import defaultdict
set_dict = defaultdict(set)
print(set_dict['key'])
>> set()
print(set_dict)
>> {'key': set()}
value값이 빈 set이다.from collections import defaultdict
d = defaultdict(list)
d['element'].append('water')
d['element'].append('fire')
d['element'].append('air')
print(d)
>> {'element': ['water', 'fire', 'air']}
print(d['element'])
>> ['water', 'fire', 'air']
key 1개 - value 1개의 매칭이라는 점. d['element'] 자체가 value이기 때문에 list에서 추가를 할 때 쓰는 append를 써야 함!만약
d = defaultdict(set)이었다면 add를 사용했을 것이다from collections import defaultdict d = defaultdict(set) d['element'].add('water') d['element'].add('fire') d['element'].add('air') print(d) >> {'element': {'air', 'water', 'fire'}}