Python for super beginners Python # dictionary type 2 for super beginners

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.

Dictionary type 2

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 ofexisting 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

table of contents

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

Python for super beginners Python # dictionary type 1 for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
Python #function 2 for super beginners
Python for super beginners Python #functions 1
Python #list for super beginners
How to convert Python # type for Python super beginners: str
Python #len function for super beginners
Python #Hello World for super beginners
How to convert Python # type for Python super beginners: int, float
Let's put together Python for super beginners
python textbook for beginners
Python Dictionary Beginner's Guide
OpenCV for Python beginners
Python for super beginners Python for super beginners # Easy to get angry
Easy understanding of Python for & arrays (for super beginners)
About Python external module import <For super beginners>
Python list, for statement, dictionary
Learning flow for Python beginners
Python3 environment construction (for beginners)
Basic Python grammar for beginners
~ Tips for beginners to Python ③ ~
Dictionary type 2
Dictionary type 1
<For beginners> python library <For machine learning>
Python dictionary
[Python] Reasons for overriding using super ()
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
[Python] dictionary
Python dictionary
[Kaggle for super beginners] Titanic (Logistic regression)
[Python] Minutes of study meeting for beginners (7/15)
Type annotations for Python2 in stub files!
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Atcoder standard input set for beginners (python)
[For beginners] Try web scraping with Python
A textbook for beginners made by Python beginners
2016-10-30 else for Python3> for:
python [for myself]
[Python] Memo dictionary
[Python] Dictionary (hash)
Python basics: dictionary
Beginners practice Python
Python # string type
Python beginner's note
Memo # 4 for Python beginners to read "Detailed Python Grammar"
The fastest way for beginners to master Python
I made a python dictionary file for Neocomplete
Causal reasoning and causal search with Python (for beginners)
For new students (Recommended efforts for Python beginners Part 1)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Get multiple maximum keys in Python dictionary type
I compared "python dictionary type" and "excel function"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
Try to calculate RPN in Python (for beginners)
Memo # 2 for Python beginners to read "Detailed Python Grammar"
How to get dictionary type elements of Python 2.7
Memo # 7 for Python beginners to read "Detailed Python Grammar"