Get the value of a specific key in a list from the dictionary type in the list with Python

A note on the ** basics ** of Python: memo: I'm a Python amateur, so please kindly tell me if there are any mistakes. : penguin:


hashira = [{'Name': 'Yoshiyuki Tomioka', 'Kokyuu': 'water' }, 
           {'Name': 'Butterfly Shinobu', 'Kokyuu': 'Bug' },
           {'Name': 'Purgatory Anjuro', 'Kokyuu': 'flame' },
           {'Name': 'Umeda Tengen', 'Kokyuu': 'sound' },
           {'Name': 'Tokito Muichiro', 'Kokyuu': 'haze' },
           {'Name': 'Honeydew Temple Honeydew', 'Kokyuu': 'love' },
           {'Name': 'Screaming 嶼 冥', 'Kokyuu': 'rock' },
           {'Name': 'Ikuro Kobayashi', 'Kokyuu': 'snake' },
           {'Name': 'Immortal river', 'Kokyuu': 'Wind' }]

Get the name of the pillar!

name = [d.get('Name') for d in hashira]
print(name)
# => ['Yoshiyuki Tomioka', 'Butterfly Shinobu', 'Purgatory Anjuro', 'Umeda Tengen', 'Tokito Muichiro', 'Honeydew Temple Honeydew', 'Screaming 嶼 冥', 'Ikuro Kobayashi', 'Immortal river']

If you have a common key, you can get it by value with [key name] instead of get (key name)!

kokyuu = [d['Kokyuu'] for d in hashira]
print(kokyuu)
# => ['water', 'Bug', 'flame', 'sound', 'haze', 'love', 'rock', 'snake', 'Wind']

Next are the main characters


kisatsutai = [{'Name': 'Tanjiro Kamado', 'Kokyuu': 'water' },
              {'Name': 'Mameko' }, 
              {'Name': 'Inosuke Hashibira', 'Kokyuu': 'beast' },
              {'Name': 'Zenitsu Agatsuma', 'Kokyuu': 'Thunder' }]

#Sadako cannot breathe.

Let's get Kokyuu like a pillar.

name = [d.get('Name') for d in kisatsutai]
print(name)
# ['water', None, 'beast', 'Thunder']

As you can see, the get () method returns None by default if the key doesn't exist.

It is also possible to pass the default value in the second argument of get ()

name = [d.get('Name', 'Moo') for d in hashira]
print(name)
# => ['water', 'Moo', 'beast', 'Thunder']

So what if [key name] instead of get (key name)? An error will occur if there is an element that does not have the specified key.

kokyuu = [d['Kokyuu'] for d in kisatsutai]
print(kokyuu)
# => KeyError: 'Kokyuu'

It is also possible to exclude elements that do not have the specified key using the if statement!

name = [d['Kokyuu'] for d in kisatsutai if 'Kokyuu' in d]

print(name)
# => ['water', 'beast', 'Thunder']

As mentioned above, the Python amateur who saw Kimetsu no Yaiba the Movie twice has sent it. => MX/4D was fun.

【reference】 · Get a list of specific key values ​​from a list of dictionaries in Python

Recommended Posts

Get the value of a specific key in a list from the dictionary type in the list with Python
Get the value of a specific key up to the specified index in the dictionary list in Python
Get the number of specific elements in a python list
How to get a list of files in the same directory with python
How to get the last (last) value in a list in Python
Get a list of files in a folder with python without a path
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Make a copy of the list in Python
Sort dict in dict (dictionary in dictionary) with a specific key
Get a list of packages installed in your current environment with python
Receive a list of the results of parallel processing in Python with starmap
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
[python] Get the list of classes defined in the module
How to write a list / dictionary type of Python3
How to identify the element with the smallest number of characters in a Python list?
Different from the import type of python. from A import B meaning
Get a list of purchased DMM eBooks with Python + Selenium
A memo when checking whether the specified key exists in the defined dictionary with python
Get the value while specifying the default value from dict in Python
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Python: Create a dictionary from a list of keys and values
Do not specify a mutable object (list type, dictionary type, etc.) as the initial value of the function argument of python.
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
Get index of nth largest / smallest value in list in Python
Extract the value closest to a value from a Python list element
I tried to create a Python script to get the value of a cell in Microsoft Excel
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Try to get a list of breaking news threads in Python.
How to check the memory size of a dictionary in Python
[Python] Get the update date of a news article from HTML
[python] Get the rank of the values in List in ascending / descending order
Read a file in Python with a relative path from the program
[Python] How to get a value with a key other than value with Enum
Get the number of readers of a treatise on Mendeley in Python
Display a list of alphabets in Python 3
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Get the EDINET code list in Python
[Python] Get a list of folders only
The story of creating a bot that displays active members in a specific channel of slack with python
Get a capture of the entire web page in Selenium Python VBA
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Generate a list packed with the number of days in the current month.
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Linux] Command to get a list of commands executed in the past
How to get a value from a parameter store in lambda (using python)
Get a datetime instance at any time of the day in Python
Get the average salary of a job with specified conditions from indeed.com
Get the return value of an external shell script (ls) with python3
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
[Python] Sort the list of pathlib.Path in natural sort
Get multiple maximum keys in Python dictionary type
Get only the subclass elements in a list
Find the divisor of the value entered in python
Get a list of IAM users with Boto3
How to get dictionary type elements of Python 2.7