Basic grammar of Python3 system (dictionary)

Overview

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.

Dictionary (dict)

--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 a dictionary

>>> #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 to dictionary by dict ()

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

Get, add, or change elements with [key]

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

Combine dictionaries with update ()

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

Deleting elements with del

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> del dict1["key2"]
>>> dict1
{'key1': 'val1', 'key3': 'val3'}

Delete all elements with clear ()

>>> # 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
{}

Check if there is a key value by in

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
... }
>>> "key1" in dict1
True
>>> "key3" in dict1
False

Convert to a list according to usage by keys (), values (), items ()

>>> 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')]

Copy the dictionary with copy ()

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> dict2 = dict1.copy()
>>> dict2
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

Recommended Posts

Basic grammar of Python3 system (dictionary)
Basic grammar of Python3 system (character string)
Basic grammar of Python3 system (included notation)
Python3 basic grammar
Basic grammar of Python3 series (list, tuple)
Python basic grammar / algorithm
Python Basic Course (7 Dictionary)
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Python basic grammar memo
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
Basic operation list of Python3 list, tuple, dictionary, set
Basic Python 3 grammar (some Python iterations)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
I wrote the basic grammar of Python with Jupyter Lab
Python dictionary
[Python] dictionary
Python dictionary
Basic study of OpenCV with Python
[Basic grammar] Differences between Ruby / Python / PHP
Python Basic Course (at the end of 15)
Memo of troubles about coexistence of Python 2/3 system
Status of each Python processing system in 2020
[Python] Memo dictionary
Introduction of Python
RF Python Basic_01
python grammar check
Comparing the basic grammar of Python and Go in an easy-to-understand manner
[Python] Dictionary (hash)
Python basics: dictionary
Basic Python writing
Basics of Python ①
Basics of python ①
Copy of python
RF Python Basic_02
Introduction of Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
VBA user tried using Python / R: basic grammar
How to get dictionary type elements of Python 2.7
This is the only basic review of Python ~ 3 ~
Comparison of Python and Ruby (Environment / Grammar / Literal)
Basic story of inheritance in Python (for beginners)
[Introduction to Python] Basic usage of lambda expressions
Basic operation of Python Pandas Series and Dataframe (1)
What you want to memorize with the basic "string manipulation" grammar of python
[Go] Basic grammar ① Definition
[Python] Operation of enumerate
List of python modules