Python> Comprehension / Comprehension> List comprehension

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 2285 / 12833)

Comprehension make it possible for you to combine loops and conditional tests with a less verbose syntax.

The simplest form

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 2319 / 12833)

The simplest form of list comprehension is:

[ expression for item in iterable ]

I implemented it with reference to the example. http://ideone.com/9l9LpH

number_list = [ 2 * number - 1 for number in range(1,5)]
print(number_list)

result


[1, 3, 5, 7]

include a conditional expression

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 2319 / 12833)

[ expression for item in iterable if condition ]

http://ideone.com/A1AkaE

number_list = [ number for number in range(1,9) if number % 2 == 1]
print(number_list)

result


[1, 3, 5, 7]

Japanese translation

It sounds like a comprehension.

http://qiita.com/7of9/items/d03d099b400d9a067086#comment-a3d81d6be1e59c4a0a35 http://qiita.com/taiga_6404/items/20a3d9a7edf1f7bd6a2c http://qiita.com/supersaiakujin/items/0776d3252c5000ca146e

You can also follow the for statement with the for statement.

Recommended Posts

Python> Comprehension / Comprehension> List comprehension
Python list comprehension speed
Python comprehension
List comprehension
Python comprehension
List comprehension
[Python] list
Python basics: list
Note: List comprehension
Python list manipulation
Python basic operation 1st: List comprehension notation
List of python modules
Python> list> extend () or + =
FizzBuzz in list comprehension
Filter List in Python
python unittest assertXXX list
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
Not just list comprehension
List find in Python
Python exception class list
Quicksort 2 | Easy list comprehension
Initialize list with python
[Python] List Comprehension Various ways to create a list
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
Python hand play (two-dimensional list)
Python list, for statement, dictionary
Summary of Python3 list operations
Python
[Python] How to use list 1
Create ToDo List [Python Django]
Specify multiple list indexes (Python)
Python Basic Course (5 List Tuples)
Python list is not a list
list comprehension because operator.methodcaller cannot be used in python 2.5
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Python #list for super beginners
Python3 comprehension (List, dictionary) that I have seen somewhere
Getting list elements in Python
[Road to intermediate Python] Use if statement in list comprehension
Extract multiple list duplicates in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Convert list to DataFrame with python
[Python beginner] Divide one list (5 lines).
python / Make a dict from a list.
[Python] How to use list 3 Added
Write python list fast vim tips
Python3> List generation from iterable> list (range (5))
Delete multiple elements in python list
About the basics list of Python basics
kafka python
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension