Summary of dictionary type array.
Variable name = {key 1: element 1, key 2: element 2, ,,,,,}
└ {} Enclose in curly braces
└ Give each element a name (key)
└ Connect keys and elements with ":" (writing style similar to css)
└ Element extraction is specified by key
└ Operators can also be used for keys and elements
└ Elements are in the same order (cannot be specified by index number)
python
lists1 = {'a':'AAA', 'b':'BBB', 'c':'CCC'}
print(lists1)
{'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
print(lists1['c'])
CCC
python
lists2 = {1:111, 2:222, 3:333}
print(lists2)
{1: 111, 2: 222, 3: 333}
print(lists2[2])
222
It is considered as the value of the key, not the index number (because the key can also be set to a number)
lists3 = {1:5*6, 'a':30/6, 2*8:'AAA'+'BBB'}
print(lists3)
{1: 30, 'a': 5.0, 16: 'AAABBB'}
print(lists3[1])
30
print(lists3['a'])
5.0
print(lists3[16])
AAABBB
The processed value is stored in the array
x=1000
y='try'
listsA = {'a':'AAA', 'b':'BBB', 'c':'CCC'}
listsB = [111, 222, 333]
lists4 = {'a':x, 'b':y, 'c':listsA, 'd':listsB}
print(lists4)
{'a': 1000, 'b': 'try', 'c': {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}, 'd': [111, 222, 333]}
You can also use array variables.
lists5 = {5:'CCC', 5:'BBB', 5:'AAA', 5:333, 5:999}
print(lists5)
{5: 999}
print(lists5[5])
999
No error occurs. Only the last one element is stored. * Overwrite at the beginning or in order
python
lists = {'a':'AAA', 'b':'BBB', 'c':'CCC'}
print(lists['f'])
KeyError: 'f'
Error type: KeyError
python
lists = {'a':'AAA', 'b':'BBB', 'c':'CCC'}
print(lists[2])
KeyError: 2
Error type: KeyError It is considered as the key value, not the index number. Error because key = 2 does not exist
lists = ['a':'AAA', 'b':'BBB', 'c':'CCC']
print(lists)
SyntaxError: invalid syntax
Error type: SyntaxError {} An error will occur if you use parentheses [] instead of curly braces.
lists1 = {'a':'AAA', b:'BBB', 'c':'CCC'}
print(lists1)
NameError: name 'b' is not defined
Error type: NameError Considered a variable. An error will occur because it is not defined.
Array name [key name you want to add] = value you want to add
lists1 = {'a':'AAA', 'b':'BBB', 'c':'CCC'}
#add to
lists1['d']='ddd'
lists1[3]=333
print(lists1)
{'a': 'AAA', 'b': 'BBB', 'c': 'CCC', 'd': 'ddd', 3: 333}
Array name [key name you want to change] = value you want to change
└ Overwrite the original element
└ Key name cannot be changed
lists1 = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
#Change
lists1['b']=999
lists1['c']='GGG'
print(lists1)
{'a': 'AAA', 'b': 999, 'c': 'GGG'}
del array name [key name you want to delete]
└ For non-existent keys KeyError
lists1 = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
del lists1['c']
print(lists1)
{'a': 'AAA', 'b': 'BBB'}
Array name.pop (key name you want to delete)
└ For non-existent keys KeyError
lists1 = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
del lists1['a']
print(lists1)
{'b': 'BBB', 'c': 'CCC'}
Array name.pop (key name you want to delete)
└ For non-existent array NameError
lists1 = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
lists1.clear()
print(lists1)
{}
for [Name given to the key] in [Array name]:
processing
└ The name given to the key is arbitrary. Valid only in for statements
└ It is the key (not the value) to extract one by one
└ Get value array name [name given to key]
▼ Example
lists = {'a': 'AAA', 'b': 'BBB', 'c': 'CCC'}
for list_key in lists:
print(f'Key:{list_key}value:{lists[list_key]}')
#output
Key: a value:AAA
Key: b value:BBB
Key: c value:CCC