[Road to intermediate Python] Use if statement in list comprehension

Link to summary

https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8

Introduction

In order to study Python, I copied a swarm intelligence library called acopy.

In acopy, many interesting Python grammars and idioms are used, and it is summarized that it is convenient among them.

This time, we will learn the if statement of Python list comprehension.

How to write

By using the if statement in the list comprehension notation, you can select the value to be used in the list.

For example, if you write a list that identifies even numbers contained in $ [0, N) $

N = int(input())

odds = [x for x in range(N) if x % 2]
print(odds)

You can write as above. It only determines x if the condition ʻif x% 2` is met.

In other words

N = int(input())

arr = []
for x in range(N):
    if x % 2 == 0:
        arr.append(x)

The above is the equivalent code.

Combine with ternary operator

Can be combined with the ternary operator.

Does the condition attached to ** right side ** in list comprehension judge the value? Would you like to? It is ** selection ** of.

The ternary operator, on the other hand, determines the condition from the screened values and changes the value returned for True and False.

It returns an even number as before, but when the ones digit is 0, let's return a list containing $ 0 $ in $ x $.


N = int(input())

'''
[[], 2, 4, 6, 8, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 12, 14, 16, 18, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 22, 24, 26, 28, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
'''
evens = [x if x % 10 else [0] * x for x in range(N) if x % 2 == 0]
print(evens)

The if on the back side after for judges whether it is an even number and sorts it. After passing through the sort, the ternary operator is used to return an array containing $ 0 $ and $ x $ if the ones place is not 0.

To be honest, in the above example, there is no point in using it.

Recommended Posts

[Road to intermediate Python] Use if statement in list comprehension
[Road to intermediate Python] Use ternary operators
[Road to intermediate Python] Use lambda expressions
A road to intermediate Python
[Python] How to use list 1
[Road to Python Intermediate] Define __getattr__ function in class
[Road to intermediate Python] Define in in your own class
[Road to intermediate Python] Install packages in bulk with pip
How to use SQLite in Python
[Python] How to write an if statement in one sentence.
[Python] How to use list 3 Added
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
[Road to Intermediate] Understanding Python Properties
[Introduction to Python] How to use the in operator in a for statement?
Difference in how to write if statement between ruby ​​and python
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 33. if statement
Easy way to use Wikipedia in Python
How to use __slots__ in Python class
Summary of how to use Python list
How to use regular expressions in Python
How to use is and == in Python
[Road to intermediate Python] Article link summary
Python> Comprehension / Comprehension> List comprehension
[Python] if statement
What to do if you can't use scikit grid search in Python
If you want to count words in Python, it's convenient to use Counter.
[Python] List Comprehension Various ways to create a list
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
[Road to Intermediate] Python seems to be all objects
Use cryptography module to handle OpenSSL in Python
[Algorithm x Python] How to use the list
Don't use readlines () in your Python for statement!
How to use tkinter with python in pyenv
Use os.getenv to get environment variables in Python
How to remove duplicate elements in Python3 list
Sorted list in Python
Python Exercise 2 --List Comprehension
Use config.ini in Python
FizzBuzz in list comprehension
Use dates in Python
Python list comprehension speed
Filter List in Python
Use Valgrind in Python
List find in Python
How to use list []
Use profiler in Python
If ... else in comprehension
Python basic if statement
[For beginners] How to use say command in python!
Things to note when initializing a list in Python
list comprehension because operator.methodcaller cannot be used in python 2.5
[Python] How to sort dict in list and instance in list
Convenient to use matplotlib subplots in a for statement
I tried to summarize how to use pandas in python
How to use the model learned in Lobe in Python
[Python] How to output the list values in order