Python/ETC

[python] 파이썬 딕션너리 함수 (dictionary Methods)

꼰대 2021. 5. 17. 22:49

1. clear()

- dict내 모든 데이터 삭제

- 참고 : https://www.w3schools.com/python/ref_dictionary_clear.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}
>> car.clear()
>> print(car)

{}


2. copy()

- dict를 복사하여 반환

- copy()는 object 자체를 복사하는 것으로 원본 object의 데이터가 바뀌어도 복사된 object의 데이터가 변하지 않음

- 반면 변수를 대입하면 변수만 바뀔 뿐 변수가 바라보는 object는 동일하므로 변경된 데이터가 동일하게 적용됨

- 참고 : https://www.w3schools.com/python/ref_dictionary_copy.asp

>> car1 = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> vehicle1 = car1.copy()

>> print(vehicle1)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

>> car1.clear()

>> print(vehicle1)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

 

>> car2 = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> vehicle2 = car2

>> print(vehicle2)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

>> car2.clear()

>> print(vehicle2)

{}


3. fromkeys(keys, value)

- 첫번째 인자로 반복 가능한 object를 받아 해당 값을 key로 새로운 dict를 생성하여 반환

- 두번째 인자가 있을 경우 해당 인자값을 모든 key의 value로 저장, 없을 경우 None

- 참고 : https://www.w3schools.com/python/ref_dictionary_fromkeys.asp

>> x = ('key1', 'key2', 'key3')

>> y = [1, 2, 3]

>> thisdict = dict.fromkeys(x, y)

>> print(thisdict)

{'key1': [1, 2, 3], 'key2': [1, 2, 3], 'key3': [1, 2, 3]}


4. get(key, value)

- dict의 첫번째 인자인 key값에 해당하는 value값을 반환

- 두번째 인자는 만약 key값이 없을 경우 두번째 인자값 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_get.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.get('model')

>> print(x)

Mustang

 

>> y = car.get('price', 20000)

>> print(y)

20000


5. items()

- dict 내 key-value가 튜플형태로 이루어진 리스트 형태로 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_items.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.items()

>> print(x)

dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])


6. keys()

- dict 내 key값을 리스트 형태로 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_keys.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.keys()

>> print(x)

dict_keys(['brand', 'model', 'year'])


7. pop(key, value)

- dict 내 첫번째 인자로 받은 key값에 해당하는 데이터 삭제

- 첫번째 인자로 받은 key값의 데이터가 없으면 Exception 발생

- 만약 첫번째 인자로 받은 key값의 데이터가 없고 두번째 인자 value가 있을 경우 해당값을 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_pop.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> car.pop('brand')

>> print(car)

{"model": "Mustang", "year": 1964}

 

>> car2 = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car2.pop('price', False)

>> print(car2)

>> print(x)

{"brand": "Ford", "model": "Mustang", "year": 1964}

False


8. popitem()

- dict 내 마지막 데이터를 삭제하며 삭제된 데이터는 (key, value) 형태의 튜플로 반환

- python 3.7이전 버전에서는 랜덤 삭제

- 참고 : https://www.w3schools.com/python/ref_dictionary_popitem.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.popitem()

>> print(car)

>> print(x)

{"brand": "Ford", "model": "Mustang"}

("year", 1964)


9. setdefault(key, value)

- dict 내 첫번째 인자로 받은 key값에 해당하는 데이터 반환하며 없으면 None

- 만약 첫번째 인자로 받은 key값에 해당하는 데이터가 없고 두번쨰 인자값이 있다면 해당 인자값을 dict에 저장 후 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_setdefault.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.setdefault('model')

>> y = car.setdefault('price', 20000)

>> print(x)

>> print(y)

>> print(car)

Mustang

20000

{"brand": "Ford", "model": "Mustang", "year": 1964, "price":20000}


10. update(iterable)

- key:value로 이루어진 dict 형태의 object를 인자로 받아 dict에 추가

- 참고 : https://www.w3schools.com/python/ref_dictionary_update.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> a = {"price":20000}

>> car.update(a)

>> print(car)

{"brand": "Ford", "model": "Mustang", "year": 1964, "price":20000}


11. values()

- dict 내 모든 value값을 리스트 형태의 반복 가능한 object로 반환

- 참고 : https://www.w3schools.com/python/ref_dictionary_values.asp

>> car = {"brand": "Ford", "model": "Mustang", "year": 1964}

>> x = car.values()

>> print(x)

dict_values(['Ford', 'Mustang', 1964])

 

>> car['year'] = 2000

>> print(x)

dict_values(['Ford', 'Mustang', 2000])

반응형