[개념] Chapter03-5 : 딕셔너리

Date:     Updated:

카테고리:

태그:

인프런 강의 프로그래밍 시작하기 : 파이썬 입문을 듣고 정리한 내용입니다✏️.

딕셔너리 자료형(순서X, 키 중복X, 수정O, 삭제O)

선언

a = {'name': 'Kim', 'phone': '01033337777', 'birth': '870514'}
b = {0: 'Hello Python'}
c = {'arr': [1, 2, 3, 4]}
d = {
    'Name': 'Niceman',
    'City': 'Seoul',
    'Age': 33,
    'Grade': 'A',
    'Status': True
}
e = dict([
    ('Name', 'Niceman'),
    ('City', 'Seoul'),
    ('Age', 33),
    ('Grade', 'A'),
    ('Status', True)
])

# 이것을 주로 사용
f = dict(
    Name = 'Niceman',
    City = 'Seoul',
    Age = 33,
    Grade = 'A',
    Status = True
)

출력

print('a -', type(a), a)
# a - <class 'dict'> {'name': 'Kim', 'phone': '01033337777', 'birth': '870514'}

print('b -', type(b), b)
# b - <class 'dict'> {0: 'Hello Python'}

print('c -', type(c), c)
# c - <class 'dict'> {'arr': [1, 2, 3, 4]}

print('d -', type(d), d)
# d - <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}

print('e -', type(e), e)
# e - <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}

print('f -', type(f), f) # f - <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A', 'Status': True}


# Key 존재X -> 에러발생
print('a -', a['name']) # a - Kim

# get함수 : Key 존재O -> None 처리
# 예외처리를 위해 주로 사용
print('a -', a.get('name')) # a - Kim
print('a -', a.get('name1')) # a - None


print('b -', b[0]) # b - Hello Python
print('b -', b.get(0)) # b - Hello Python

print('f -', f.get('City')) # f - Seoul
print('f -', f.get('Age')) # f - 33

수정 & 추가

수정

있으면 value 수정

a['address'] = 'seoul'
print('a -', a) # a - {'name': 'Kim', 'phone': '01033337777', 'birth': '870514', 'address': 'seoul'}

a['rank'] = [1,2,3]
print('a -', a) # a - {'name': 'Kim', 'phone': '01033337777', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3]}

a.update(birth = '910904')
print('a -', a) # a - {'phone': '01033337777', 'birth': '910904', 'address': 'dj', 'rank': [1, 2, 3], 'test': 'test_dict'}

temp = {'address' : 'Busan'}
a.update(temp)
print('a -', a) # a - {'phone': '01033337777', 'birth': '910904', 'address': 'Busan', 'rank': [1, 2, 3], 'test': 'test_dict'}

추가

value 없으면 추가

a['test'] = 'test_dict'
print('a -', a) # a - {'phone': '01033337777', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3], 'test': 'test_dict'}

함수

dict_keys, dict_values, dict_items : 반복문(iter)에서 사용 가능

keys()

print('a -', a.keys()) # a - dict_keys(['name', 'phone', 'birth', 'address', 'rank'])
print('b -', b.keys()) # b - dict_keys([0])
print('c -', c.keys()) # c - dict_keys(['arr'])
print('d -', d.keys()) # d - dict_keys(['Name', 'City', 'Age', 'Grade', 'Status'])

print('a -', list(a.keys())) # a - ['name', 'phone', 'birth', 'address', 'rank']
print('b -', list(b.keys())) # b - [0]

values()

print('a -', a.values()) # a - dict_values(['Kim', '01033337777', '870514', 'seoul', [1, 2, 3]])
print('b -', b.values()) # b - dict_values(['Hello Python'])
print('c -', c.values()) # c - dict_values([[1, 2, 3, 4]])

print('a -', list(a.values())) # a - ['Kim', '01033337777', '870514', 'seoul', [1, 2, 3]]
print('b -', list(b.values())) # b - ['Hello Python']

items()

print('a -', a.items()) # a - dict_items([('name', 'Kim'), ('phone', '01033337777'), ('birth', '870514'), ('address',
# 'seoul'), ('rank', [1, 2, 3])])
print('b -', b.items()) # b - dict_items([(0, 'Hello Python')])
print('c -', c.items()) # c - dict_items([('arr', [1, 2, 3, 4])])

print('a -', list(a.items())) # a - [('name', 'Kim'), ('phone', '01033337777'), ('birth', '870514'), ('address', 'seoul'), ('rank', [1, 2, 3])]
print('b -', list(b.items())) # b - [(0, 'Hello Python')]

pop() & popitem()

print('a -', a.pop('name'))
print('a -', a) # a - {'phone': '01033337777', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3]}


print('c -', c.pop('arr'))
print('c -', c) # c - {}


print('f -', f.popitem())
print('f -', f) # f - {'Name': 'Niceman', 'City': 'Seoul', 'Age': 33, 'Grade': 'A'}

in

print('a -', 'birth' in a) # a - True
print('a -', 'birth2' in a) # a - False

print('b -', 'City' in d) # b - True

맨 위로 이동하기

Python 카테고리 내 다른 글 보러가기

댓글남기기