[Python] Chapter 04-07 Various data structures (dictionary manipulation)

[Python] Chapter 04-07 Dictionary Manipulation

The dictionary explained that the keys are immutable and cannot be manipulated, but the values are mutable and can be manipulated. This time I would like to explain the operation of the dictionary.

Addition of dictionary elements

First, let's talk about adding elements. Enter the following code from the ** Python Console **.

>>> foodD = {'a' : 'apple', 'b' : 'banana', 'c': 'cake'}
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake'}

Up to this point, as explained last time, it is the creation of a dictionary. Now, let's add an element to the dictionary variable ** foodD **, but there are two ways to add it.

How to add one element

First, I will explain how to add one element. Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.

>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake'}
>>> foodD['d'] = 'dragon fruits'
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake', 'd': 'dragon fruits'}

When adding an element to the dictionary, add a new element in the form of ** variable name [key] = value **.

But what if you already have the key? Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.

>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'cake', 'd': 'dragon fruits'}
>>> foodD['c'] = 'carrot'
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits'}

You can see that the **'c' ** part has been overwritten. In fact, you cannot specify ** duplicate keys for one dictionary. ** If there are duplicates, you will overwrite another value.

How to add multiple elements

To add multiple elements to the dictionary, use the ** update method **. Enter the following code in the ** Python Console **. Display the contents of the variable ** foodD ** once and then execute.

>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits'}
>>> foodD.update({'e' : 'egg', 'f' : 'fried potato'})
>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}

It will be in the form of merging another dictionary. If you check ** foodD **, you can see that 'e' and **'f' ** have been added.

Delete dictionary elements

Dictionary elements can also be deleted by specifying a key. In fact, like a list, you can delete it using the ** del statement **. Enter the following code in the ** Python console **. Display the contents of the variable ** foodD ** once and then execute.

>>> foodD
{'a': 'apple', 'b': 'banana', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
>>> del foodD['b']
>>> foodD
{'a': 'apple', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}

I was able to confirm that 'b' was deleted.

Also, to remove all elements in the dictionary, use the ** clear method ** as you would for a list.

>>> foodD
{'a': 'apple', 'c': 'carrot', 'd': 'dragon fruits', 'e': 'egg', 'f': 'fried potato'}
>>> foodD.clear()
>>> foodD
{}

You can see that all the elements in the dictionary have been removed and ** {} ** is output, which is empty.

Exploring dictionary elements

You can also search for elements in the dictionary. This time as well, we will search by specifying the key. In the previous section, the search was performed by specifying the ** variable name [key] **, but if there is no corresponding key, an error will be output. This time, I will explain how to output no error even if there is no key.

Enter the following code in the ** Python console **. First, substitute the dictionary for ** D **.

>>> D = {'NRT' : 'Narita Airport', 'CTS' : 'New Chitose Airport', 'HIJ' : 'Hiroshima Airport'}
>>> D
{'NRT': 'Narita Airport', 'CTS': 'New Chitose Airport', 'HIJ': 'Hiroshima Airport'}

Use the ** get method ** as a way to avoid getting an error even if you don't have the key.

>>> D
{'NRT': 'Narita Airport', 'CTS': 'New Chitose Airport', 'HIJ': 'Hiroshima Airport'}
>>> D.get('NRT')
'Narita Airport'
>>> D.get('MYJ')

First, 'NRT' exists in ** D **, so **'Narita Airport' ** will be returned. 'MYJ' does not exist in ** D **, so nothing is returned.

By the way, if you use the print function to specify a key that does not exist, ** None ** will be returned.

>>> print(D.get('MYJ'))
None

You can also check the existence by using the in operator explained at the time of listing. Unlike the list, specify the key to check.

>>>'NRT' in D
True
>>>'MYJ' in D
False

Finally

I've looked through the dictionary, but I think I've confirmed that it's basically the same operation as the list. The difference is that the dictionary specifies the key to the list that specifies the number of the element. It is a data structure that will appear frequently in the future, so let's hold it down.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 04-07 Various data structures (dictionary manipulation)
[Python] Chapter 04-02 Various data structures (list manipulation)
[Python] Chapter 04-06 Various data structures (creating dictionaries)
[Python] Chapter 04-03 Various data structures (multidimensional list)
[Python] Chapter 04-04 Various data structures (see list)
[Python] Chapter 04-05 Various data structures (tuple creation and features)
[Python] Chapter 04-01 Various data structures (list creation and element retrieval)
[Python] [Supplement] Chapter 04-08 Various data structures (creating and manipulating sets)
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Python for Data Analysis Chapter 4
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3
Python data structures learned with chemoinformatics
Python dictionary
[Python] dictionary
Python dictionary
Python Application: Data Visualization Part 3: Various Graphs
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Python] Various data processing using Numpy arrays
Let's do MySQL data manipulation with Python
[Python] Memo dictionary
Python Grammar-String Manipulation
Data analysis python
[Python] Dictionary (hash)
Python basics: dictionary
# 3 [python3] Various operators
PySpark data manipulation
Python Application: Data Handling Part 2: Parsing Various Data Formats
Python list manipulation
[python] Read data
Receive dictionary data from a Python program in AppleScript
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Python Basic Course (7 Dictionary)
Python Node.js character manipulation
Python Data Visualization Libraries
String manipulation in python
Data analysis using Python 0
Data analysis overview python
Date manipulation in Python
Various Python visualization tools
Data cleaning using Python
Python3 List / dictionary memo
Python Dictionary Beginner's Guide
Data manipulation with Pandas!
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
[Python] Chapter 01-01 About Python (First Python)
Data analysis with Python
Various processing of Python
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
I tried to make various "dummy data" with Python faker
Various ways to calculate the similarity between data in python
[Python] Use JSON format data as a dictionary type object
Basic summary of data manipulation with Python Pandas-First half: Data creation & manipulation