반응형
current_price = {'Daum KAKAO': 2, 'naver':1, 'wooaBrothers':3}
print(current_price)
# get all keys of dictionary object
# keys() function return type is not list object
print(current_price.keys())
# Should be change object type to list type
list(current_price.keys())
list_keys = list(current_price)
print(list_keys)
# Extract values of each item in the dictionary
list_value = list(current_price.values())
print(list_value)
# Check about Key exist in the dictionary
print('Samsung' in current_price.keys())
print('Daum KAKAO' in current_price.keys())
# Print value using by key
print(current_price['naver'])
# It will be occur error
print(current_price['Samsung'])
실행 결과
$ py dictionary_key_value.py
{'Daum KAKAO': 80000, 'naver': 800000, 'wooaBrothers': 30000}
dict_keys(['Daum KAKAO', 'naver', 'wooaBrothers'])
['Daum KAKAO', 'naver', 'wooaBrothers']
[80000, 800000, 30000]
False
True
800000
Traceback (most recent call last):
File "dictionary_key_value.py", line 30, in <module>
print(current_price['Samsung'])
KeyError: 'Samsung'
johnmark@DESKTOP-NH9EF0Q MINGW64 /d/johnmark/python_basic/basic_datatype/dictionary (master)
$ py dictionary_key_value.py
{'Daum KAKAO': 2, 'naver': 1, 'wooaBrothers': 3}
dict_keys(['Daum KAKAO', 'naver', 'wooaBrothers'])
['Daum KAKAO', 'naver', 'wooaBrothers']
[2, 1, 3]
False
True
1
Traceback (most recent call last):
File "dictionary_key_value.py", line 30, in <module>
print(current_price['Samsung'])
KeyError: 'Samsung'
반응형
'Programming > Python' 카테고리의 다른 글
[Python-Basic] List Data Type - 3 (Index) (0) | 2019.11.05 |
---|---|
[Python-Basic] List Data Type - 2 (Generate) (0) | 2019.11.05 |
[Python-Basic] List Data Type - 1(Add) (0) | 2019.11.05 |
[Python-Basic] Dictionary Data Type - 3 (0) | 2019.11.05 |
[Python-Basic] Dictionary Data Type - 1 (0) | 2019.11.05 |