Popular Posts

이은한. Powered by Blogger.

2022년 2월 13일 일요일

what is Python Dictionary


Dictionary in Python

Definition

  • One of the data structure in python.
  • has key and value binded. You can fine value when you know the key
  • very slimier with hashmap

declaration

  • use { and } to declare
  • the value can be list
  • the key can be int or string. But not list. (TypeError: unhashable type: 'list')
dictionary={"key1":"value1","key2":"value2","key3":"value3"}

how to use

  1. indexing like List
  2. get() method

difference between the indexing and get method

If key does not exist, the indexing will return error. But get method will return None.

# List처럼 인덱스를 넣기
print(dictionary["key1"]) # value1
print(dictionary["key2"]) # value2
print(dictionary["key3"]) # value3
# print(dictionary["key4"]) # KeyError: 'key4' means there is no key

# get() 메서드를 사용
print(dictionary.get("key1")) # value1
print(dictionary.get("key2")) # value2
print(dictionary.get("key3")) # value3
print(dictionary.get("key4")) # return None, it does not cause error

0 개의 댓글:

댓글 쓰기