- A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are the keys that are used to access values within a dictionary.
- Dictionary의 요지는 Key와 Value로 구성된다. (반면 List는 Index와 Element로 구성된다.)
예를 들어
release_year_dict = {"Thriller": "1982", "Back in Black": "1980", \
"The Dark Side of the Moon": "1973", "The Bodyguard": "1992", \
"Bat Out of Hell": "1977", "Their Greatest Hits (1971-1975)": "1976", \
"Saturday Night Fever": "1977", "Rumours": "1977"}
-Value를 아래와 같이 불러 올 수 있다.
print(release_year_dict['Thriller'])
>>
'1982'
-Key 값을 아래와 같이 불러 올 수 있다.
print(release_year_dict.keys())
>>
dict_keys(['Rumours', 'Back in Black', 'Thriller', 'The Bodyguard', 'Saturday Night Fever', 'The Dark Side of the Moon', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)'])
-Value 값을 아래와 같이 불러 올 수 있다.
print(release_year_dict.values() )
>>
dict_values(['1977', '1980', '1982', '1992', '1977', '1973', '1977', '1976'])
- 데이터를 추가 할때는 아래와 같이 추가 할 수도 있다.
release_year_dict['Graduation'] = '2007'
- 데이터를 삭제 위해서는 아래와 같이 del로 삭제 가능하다.
del(release_year_dict['Thriller'])
print(release_year_dict)
- 데이터를 찾기 위해서는 아래와 같이 in을 사용 할 수 있다.
print('The Bodyguard' in release_year_dict)
>>
True
'IT > Python' 카테고리의 다른 글
[파이썬] 코로나로 미세먼지 지표는 좋아졌을까? (2) | 2020.07.19 |
---|---|
[파이썬] List 관련3 (split, join) (0) | 2020.06.29 |
[파이썬] List 관련2 (extend, reverse, sort) (0) | 2020.06.29 |
[파이썬] range 구문 (0) | 2020.06.29 |
[파이썬] List 관련 (Python) (0) | 2020.06.29 |