Dictionary is a data structure with key and value.
Dictionary is internally implemented as hash table, so dictionary is a fast data structure.
Dictionary is unordered before Python 3.6, and ordered after Python 3.7.
The key of dictionary must be unique.
Creating Dictionary
There are many ways to create dictionary.
### {} method
emptyDict = {}
testDict = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
print(testDict)
# {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
### dict() function
testDict = dict([
['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']
])
print(testDict)
# {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
### Dictionary Comprehension
word = 'letters'
letter_counts = {letter: word.count(letter) for letter in set(word)}
print(letter_counts)
# {'s': 1, 't': 2, 'e': 2, 'r': 1, 'l': 1}
### from Tuple
testTuple = ('ab', 'cd', 'ef') # possible only 2 characters
testDict = dict(testTuple)
print(testDict)
# {'a': 'b', 'c': 'd', 'e': 'f'}
### from List
testList = ['ab', 'cd', 'ef'] # possible only 2 characters
testDict = dict(testList)
print(testDict)
# {'a': 'b', 'c': 'd', 'e': 'f'}
testList = [['r', 'red'], ['g', 'green'], ['b', 'blue']]
testDict = dict(testList)
print(testDict)
# {'r': 'red', 'g': 'green', 'b': 'blue'}
### from Mixed type
test = (['r', 'red'], ['g', 'green'], ['b', 'blue'])
testDict = dict(test)
print(testDict)
# {'r': 'red', 'g': 'green', 'b': 'blue'}
test = [('r', 'red'), ('g', 'green'), ('b', 'blue')]
testDict = dict(test)
print(testDict)
# {'r': 'red', 'g': 'green', 'b': 'blue'}
Getting Item from Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
### [key]
print(testDict['key2'])
# value2
### get()
print(testDict.get('key3'))
# value3
### keys()
print(testDict.keys())
# dict_keys(['key1', 'key2', 'key3'])
### values()
print(testDict.values())
# dict_values(['value1', 'value2', 'value3'])
### items()
print(testDict.items())
# dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])
Setting or Adding Item to Dictionary
testDict = {"key1": "value1", "key2": "value2"}
testDict['key1'] = 'value11'
testDict['key3'] = 'value3'
print(testDict)
# {'key1': 'value11', 'key2': 'value2', 'key3': 'value3'}
Updating Item of Dictionary
testDict1 = {"key1": "value1", "key2": "value2", "key3": "value3"}
testDict2 = {"key4": "value4", "key5": "value5"}
testDict1.update(testDict2)
print(testDict1)
# {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5'}
testDict3 = {"key2": "value22", "key6": "value6"}
testDict1.update(testDict3)
print(testDict1)
# {'key1': 'value1', 'key2': 'value22', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value6'}
Removing Item from Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
### del
del testDict['key1']
print(testDict)
# {'key2': 'value2', 'key3': 'value3'}
### clear()
testDict.clear()
print(testDict)
# {}
Checking Item of Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
print('key1' in testDict)
# True
print('key4' in testDict)
# False
Searching Items from Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
for k, v in testDict.items():
print(f"{k}: {v}")
# key1: value1
# key2: value2
# key3: value3
Getting Length of Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
print(len(testDict))
# 3
Copying Dictionary
testDict = {"key1": "value1", "key2": "value2", "key3": "value3"}
copiedDict1 = testDict # same as testDict
copiedDict2 = testDict.copy() # different
testDict['key2'] = 'value22'
print(copiedDict1)
# {'key1': 'value1', 'key2': 'value22', 'key3': 'value3'}
print(copiedDict2)
# {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Dictionary Module
defaultdict Object
When a defaultdict object queries a key that doesn't exist, it generates an item for that key based on the default value instead of an error message.
import collections
testDict = collections.defaultdict(int)
testDict['key1'] = 1
print(testDict)
# defaultdict(<class 'int'>, {'key1': 1})
testDict['key2']
print(testDict)
# defaultdict(<class 'int'>, {'key1': 1, 'key2': 0})
Counter Object
Counter object counts the number of items and returns them to the dictionary.
most_common() method returns the most frequent items.
The argument represents how many items return.
import collections
testList = [1, 2, 3, 1, 2, 4, 1]
testDict = collections.Counter(testList)
print(testDict)
# Counter({1: 3, 2: 2, 3: 1, 4: 1})
print(testDict.most_common(2))
# [(1, 3), (2, 2)]
Ordered Object
In most languages, data types using hash tables don't maintain the input order.
Python 3.6 and before were the same, so it provided an object that maintained the input order.
import collections
testList = collections.OrderedDict({})
testList['key2'] = 'value2'
testList['key1'] = 'value1'
testList['key3'] = 'value3'
print(testList)
# OrderedDict([('key2', 'value2'), ('key1', 'value1'), ('key3', 'value3')])
However, dictionary in Python 3.7 and later keep its input order.
'Python' 카테고리의 다른 글
[Python] 3 Ways of Formatted String (0) | 2021.01.22 |
---|---|
[Python] List (Array) Data Structure (0) | 2020.12.07 |
[Python] Templates Static Files in Flask (0) | 2020.10.23 |
[Python] pathlib module (Object-Oriented Filesystem Paths) (0) | 2020.10.13 |
[Python] Getting Started with Flask (0) | 2020.10.11 |
댓글