코테 공부/python

Python dictionary .get(찾는 key, defaut = None)

sweet-po 2023. 6. 9. 21:18

.get(찾는 key, defaut = None)

찾는 키의 값을 반환

dict = {'a':1, 'b':2, 'c':3, 'd':4}

print(dict.get('b')) #2
print(dict.get('e')) #None
print(dict.get('e', 0)) #0
print(dict.get('e', 'nothing')) #nothing

첫번째 매개인자가 딕셔너리의 키에 없다면 디폴트로 None 반환

만약 그 디폴트 값을 바꾸려면 두번째 인자로 반환 될 것 넣어줌