Python list comprehensions that are easy to forget

It's a list comprehension that people who see it for the first time don't understand, but once they start using it, it's a list comprehension, but if you're away from Python for a while, you may forget it, so I made a note of how to use it.

Extract only specific key values from the list of dictionaries

First, simply retrieve your name from the list of profiles.

profiles = [
    {"name": "Tanaka", "age": 17},
    {"name": "Suzuki", "age": 18},
    {"name": "Sato", "age": 19},
]
names = [p["name"] for p in profiles]
print(names)

The result looks like this.

['Tanaka', 'Suzuki', 'Sato']

Conditionally retrieve value

I will try to put in a bond according to age. Only the names over 18 years old will be taken out.

profiles = [
    {"name": "Tanaka", "age": 17},
    {"name": "Suzuki", "age": 18},
    {"name": "Sato", "age": 19},
]
names = [p["name"] for p in profiles if p["age"] >= 18]
print(names)

Tanaka is excluded because she is under 18 years old.

['Suzuki', 'Sato']

Make the result a dictionary

Let's convert the list to a dictionary.

profiles = [
    {"name": "Tanaka", "age": 17},
    {"name": "Suzuki", "age": 18},
    {"name": "Sato", "age": 19},
]
names = {p["name"]: p["age"] for p in profiles}
print(names)

It would be strange if the name was covered, but in this example it's okay.

{'Tanaka': 17, 'Sato': 18, 'Suzuki': 18}

Try to make it a little richer dictionary

It's a bit brute force, but I'll make a list of names by age.

profiles = [
    {"name": "Tanaka", "age": 17},
    {"name": "Suzuki", "age": 18},
    {"name": "Sato", "age": 18},
]
names = {
    p2["age"]: [
        p1["name"] for p1 in profiles if p1["age"] == p2["age"]
    ]
    for p2 in profiles}
print(names)

It is like this.

{17: ['Tanaka'], 18: ['Suzuki', 'Sato']}

Recommended Posts

Python list comprehensions that are easy to forget
Five useful Python data types that are easy to forget
Note that Python list comprehensions are always confusing
10 Python errors that are common to beginners
Regular expressions that are easy and solid to learn in Python
[Python] Solution to the problem that elements are linked when copying a list
Syntax that Perl users tend to forget in Python
[Python] Convert list to Pandas [Pandas]
Python notes to forget soon
[Python] How to use list 1
[Introduction to Python] <list> [edit: 2020/02/22]
Python list comprehensions and generators
Points that are easy to make mistakes when using lambda during Python loop processing
python Condition extraction from a list that I often forget
[Python3] List of sites that I referred to when I started Python
Easy way to customize Python import
Convert list to DataFrame with python
Python> list> Convert double list to single list
[Python] How to use list 3 Added
Easy to use Jupyter notebook (Python3.5)
Easy Python to learn while writing
[Python Tutorial] An Easy Introduction to Python
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
[Python] A program that rotates the contents of the list to the left
[Introduction to Udemy Python3 + Application] 18. List methods
[Python] How to convert a 2D list to a 1D list
[Introduction to Udemy Python3 + Application] 17. List operation
Easy way to use Wikipedia in Python
python> Convert tuple to list> aList = list (pi_tuple)
Python amateurs try to summarize the list ①
[Python] list
[Python] I was hooked for an hour trying to use list comprehensions
Summary of how to use Python list
[Introduction to Udemy Python3 + Application] 16. List type
[Note] Terms that are difficult to remember
[Python] I tried to analyze the characteristics of thumbnails that are easy to play on YouTube by deep learning
Python error messages are concrete and easy to understand "ga" (IndexError: list index out of range with element [0])
Python error messages are specific and easy to understand "ga" (... AAA yyy BBB)
[Python] Easy introduction to machine learning with python (SVM)
[Python] List Comprehension Various ways to create a list
[python] How to display list elements side by side
List comprehensions are no longer slow in PyPy 7.3.1
Python for super beginners Python for super beginners # Easy to get angry
Easy way to use Python 2.7 on Cent OS 6
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
List of links that machine learning beginners are learning
[Algorithm x Python] How to use the list
List of Python code to move and remember
A python amateur tries to summarize the list ②
From easy git installation to docker startup python
An easy way to call Java from Python
Convert strings to character-by-character list format with python
How to remove duplicate elements in Python3 list
Updated to Python 2.7.9
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
Python basics: list
Python is easy
Python> Comprehension / Comprehension> List comprehension
Python list manipulation
How to find the first element that matches your criteria in a Python list