{Key1:Value1, Key2:Value2, Key3:Value3, ...}
>>> dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
>>> a = {1: 'hi'}
>>> a = { 'a': [1,2,3]}
>>> a = {1:'a', 2:'b'}
>>> a[1]
'a'
>>> a[2]
'b'
>>> a = {'a':1, 'b':2}
>>> a['a']
1
>>> a['b']
2
>>> dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
>>> dic['name']
'pey'
>>> dic['phone']
'0119993323'
>>> dic['birth']
'1118'
>>> a = {1:'a', 1:'b'}
>>> a
{1: 'b'}
>>> a = {[1,2] : 'hi'}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> a = {'name': 'pey', 'phone': '0119993323', 'birth': '1118'}
>>> a.keys()
dict_keys(['name', 'phone', 'birth'])
>>> for k in a.keys():
... print(k)
...
name
phone
birth
>>> list(a.keys())
['name', 'phone', 'birth']
>>> a.values()
dict_values(['pey', '0119993323', '1118'])
>>> a.items()
dict_items([('name', 'pey'), ('phone', '0119993323'), ('birth', '1118')])
>>> a.clear()
>>> a
{}
공통점: 동일한 결괏값 반환됨
>>> a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
>>> a.get('name')
'pey'
>>> a['name']
'pey'
차이점 : 존재하지 않는 키(nokey)로 값을 가져오려고 할 경우
get :
1) None은 "거짓"을 반환해줌
2) get(x, '디폴트 값') : 찾으려는 Key 값이 없을 경우 디폴트 값을 대신
배열 : KeyError
>>> a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
get : 1)
>>> print(a.get('nokey'))
None
get : 2)
>>> a.get('foo', 'bar')
'bar'
배열 : KeyError
>>> print(a['nokey'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'nokey'
>>> a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
>>> 'name' in a
True
>>> 'email' in a
False
🐥 출처 : https://wikidocs.net/book/1 점프 투 파이썬
좋은 자료 감사합니다 ..✨