You will study the basic grammar of Python 3 by referring to "Introduction to Python 3" by O'Reilly Japan. I hope it will be helpful for those who want to study Python in the same way.
--Since the dictionary is mutable, you can add, remove, and add key / value elements. --In other languages, it is called a hash map or an associative array.
>>> #Creating an empty array[]
>>> target = {}
>>> target
{}
##Dictionary creation
>>> dict_sample = {
... "key1" : "value1",
... "key2" : "value2",
... "key3" : "value3",
... }
>>> dict_sample
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Conversion is possible if it contains a two-element sequence.
>>> # lol(List of two-element list)Convert
>>> lol = [['key1','val1'], ['key2','val2'], ['key3','val3']]
>>> dict(lol)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
>>> # lot(List of two-element tuples)Convert
>>> lot = [('key1','val1'), ('key2','val2'), ('key3','val3')]
>>> dict(lot)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
>>> # tol(Tuple of a two-element list)Convert
>>> tol = (['key1','val1'], ['key2','val2'], ['key3','val3'])
>>> dict(tol)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
>>> # los(List of two-letter strings)Convert
>>> los = ['a1','b2','c3']
>>> dict(los)
{'a': '1', 'b': '2', 'c': '3'}
>>> # tos(Two-letter tuple)Convert
>>> tos = ('a1','b2','c3')
>>> dict(tos)
{'a': '1', 'b': '2', 'c': '3'}
>>> dict1 = {
... "key1" : "value1",
... "key2" : "value2",
... }
>>> #Get element
>>> dict1['key1']
'value1'
>>> #Add element
>>> dict1['key3'] = 'value3'
>>> dict1
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
>>> #Change elements
>>> dict1['key3'] = 'VALUE3'
>>> dict1
{'key1': 'value1', 'key2': 'value2', 'key3': 'VALUE3'}
>>> dict1 = {
... "key1" : "value1",
... "key2" : "value2",
... }
>>> dict2 = {
... "key2" : "val2",
... "key3" : "val3",
... "key4" : "val4",
... }
>>> #Combine dictionaries (same key values are updated)
>>> dict1.update(dict2)
>>> dict1
{'key1': 'value1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4'}
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... "key3" : "val3",
... }
>>> del dict1["key2"]
>>> dict1
{'key1': 'val1', 'key3': 'val3'}
>>> # clear()When using
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... "key3" : "val3",
... }
>>> dict1.clear()
>>> dict1
{}
>>> #When using empty dictionary assignment
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... "key3" : "val3",
... }
>>> dict1 = {}
>>> dict1
{}
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... }
>>> "key1" in dict1
True
>>> "key3" in dict1
False
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... "key3" : "val3",
... }
>>> # dict_Returned as a keys object (a view of iterable keys)
>>> dict1.keys()
dict_keys(['key1', 'key2', 'key3'])
>>> # keys()Get all keys in list
>>> list( dict1.keys() )
['key1', 'key2', 'key3']
>>> # values()Get all values in list
>>> list( dict1.values() )
['val1', 'val2', 'val3']
>>> # items()With all keys/Get value pairs in list
>>> list( dict1.items() )
[('key1', 'val1'), ('key2', 'val2'), ('key3', 'val3')]
>>> dict1 = {
... "key1" : "val1",
... "key2" : "val2",
... "key3" : "val3",
... }
>>> dict2 = dict1.copy()
>>> dict2
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
Recommended Posts