딕셔너리는 {key:value1, key:value2... } 형태로 딕셔너리를 생성해도 되고,
딕셔너리 constructor(dict)를 이용해 생성할 수 있다.
abc = {'a':1, 'b':2, 'c':3}
print(abc)
abc = dict(a=1, b=2, c=3)
print(abc)
- key 값이 같으면, 한 개를 제외한 나머지는 무시된다.
tel = {'귀선' : '1234', '귀선' : '멋있어'}
print(tel['귀선'])
print(tel)
----
멋있어
{'귀선': '멋있어'}
- 딕셔너리를 다루다보면 해당 딕셔너리에 어떤 키들이 포함되어 있는지 알아야 할 때가 있는데, keys()라는 딕셔너리 관련 함수를 사용하면 된다.
hello_world = dict(a=1, b=2, c=3)
print(hello_world)
----
{'a': 1, 'b': 2, 'c': 3}
print(hello_world.keys())
----
dict_keys(['a', 'b', 'c'])
- dict.key를 list로 만들 수도 있다.
print(keys)
----
dict_keys(['a', 'b', 'c'])
keys = list(keys)
print(keys)
['a', 'b', 'c']
- 특정 key의 유무는 in을 사용해 확인할 수 있다.
tel = {'귀선' : '1234', '데잇' : '4567'}
print('귀선' in tel)
print('선귀' in tel)
----
True
False
덕분에 잘 정리했습니다! 감사해요 : )