environment windows7 (I want a Mac Book Pro 16inch) Visual Studio Code chrome python ver3.8.3
This article is written for beginners in programming and Python.
The dictionary type has various functions.
You can use .update
when you want to add an element to the dictionary type you have created so far.
Below is an example of adding an 80's Academy Award for Best Picture to an existing Academy Awards dictionary.
Created a new 80's Academy Award-winning dictionary ʻacademy_80s Let's apply it to the description method of
existing dictionary.update (new dictionary)`.
dict.py
academy_awards = {"Green book": 2019, "Chicago": 2003, "Titanic": 1998}
academy_80s = {"Rainman": 1989, "Platoon": 1987, "Gandhi": 1983}
academy_awards.update(academy_80s)
print(academy_awards)
#{'Green book': 2019, 'Chicago': 2003, 'Titanic': 1998, 'Rainma
#Down': 1989, 'プラトーDown': 1987, 'ガDownジー': 1983}
This adds 80's Academy Awards to the existing Academy Awards dictionary.
In addition to adding multiple elements, you can add one element with existing dictionary [new key] = new value corresponding to the new key
.
dict.py
academy_awards = {"Green book": 2019, "Chicago": 2003, "Titanic": 1998}
academy_awards["Rocky"] = 1977
print(academy_awards)
#{'Green book': 2019, 'Chicago': 2003, 'Titanic': 1998, 'Rocky': 1977}
Also, overwriting the existing dictionary is existing dictionary [existing key] = new value
.
Let's overwrite the award year of Chicago with 1979.
dict.py
academy_awards = {"Green book": 2019, "Chicago": 2003, "Titanic": 1998}
academy_awards["Chicago"] = 1979
print(academy_awards)
#{'Green book': 2019, 'Chicago': 1979, 'Titanic': 1998, 'Rocky': 1977}
You can also add elements using .setdefault
. But with setdefault
, the existing
Elements cannot be updated.
I will add Deer Hunter with the year of award.
Then, I will add or overwrite the existing element Chicago with the year of award.
dict.py
academy_awards = {"Green book": 2019, "Chicago": 2003, "Titanic": 1998}
academy_awards.setdefault("Deer Hunter", 1979)
print(academy_awards)
##{'Green book': 2019, 'Chicago': 2003, 'Titanic': 1998, 'Deer Hunter': 1979}
academy_awards.setdefault("Chicago", 1968)
print(academy_awards)
#{'Green book': 2019, 'Chicago': 2003, 'Titanic': 1998, 'Deer Hunter': 1979}
#I tried to overwrite Chicago with 1968, but it's still 2003 as in the existing dictionary.
The additional overwrite of the above elements ends as dictionary type 2.
fin
Python #Hello World for super beginners
Python for super beginners and super beginners # Easy to get confused
Python #type (type) and how to check type (type) for super beginners of Python
How to convert Python # type for Python super beginners: str edition
How to convert Python # type for Python super beginners: int, float edition
Read Python # .txt file for super beginners in Python with working .py
Python #Function 1 for Python Super Beginners
Python #Function 2 for super beginners of Python
[Python #len function for super beginners of Python] (https://qiita.com/Macchino5/items/a64347f9e832406d3c24)
Python #list for super beginners of Python
Python #index for super beginners, slices
Python #dictionary type 1 for super beginners of Python
Recommended Posts