본문 바로가기

IT/Python

[파이썬] Dictionary (딕셔너리)

- 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