what is Python Dictionary

what is Python Dictionary

·

1 min read

Definition

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

how to use

dictionary={"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.get("key3")) # value3

Example

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"}

insert like List

print(dictionary["key1"]) # value1
print(dictionary["key2"]) # value2
print(dictionary["key3"]) # value3
# print(dictionary["key4"]) # KeyError: 'key4' means there is no key

using 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