[Python] List Comprehension Various ways to create a list

List Comprehension List Comprehension makes it easy to create a List. Comprehension means "include" rather than "understand", and in Japanese it seems to be called a list comprehension.

Example) Make a list of values from 0 to 29

Without List Comprehension

In [1]: L1 = []
   ...: for x in range(30):
   ...:     L1.append(x**2)    
   ...: 
   ...: print(L1)
   ...: 
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841]

When using List Comprehension

Even if you don't create an empty List and append it, you can apply it in one line as shown below.

In [2]: L2 =[x**2 for x in range(30)]
In [3]: print(L2)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841]

An if statement can follow a for statement.

In [4]: L3 = [x for x in range(30) if x % 2 == 0]
In [5]: print(L3)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

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

In [6]: L4 = [(x,y) for x in range(10) for y in range(x,10)]
In [7]: print(L4)
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 6), (6, 7), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 8), (8, 9), (9, 9)]

Generator Comprehension

Generator can be created by changing [] to ()

In [8]: G4 = ((x,y) for x in range(10) for y in range(x,10))

In [9]: G4
Out[9]: <generator object <genexpr> at 0x000000000A395D80>

In [10]: L5 = list(G4)

In [11]: print(L5)
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 6), (6, 7), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 8), (8, 9), (9, 9)]

See here for details.

Recommended Posts

[Python] List Comprehension Various ways to create a list
5 Ways to Create a Python Chatbot
Various ways to create a dictionary (memories)
[python] Create a list of various character types
Python> Comprehension / Comprehension> List comprehension
[Python] How to convert a 2D list to a 1D list
I tried to create a list of prime numbers with python
Various ways to create an array of numbers from 1 to 10 in Python.
Python Exercise 2 --List Comprehension
Edit Excel from Python to create a PivotTable
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
Create a Python module
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
Python list comprehension speed
I want to create a window in Python
How to create a JSON file in Python
Steps to create a Twitter bot with python
Create a Python environment
A python amateur tries to summarize the list ②
Various ways to read the last line of a csv file in Python
Various ways to extract columns in a NumPy array
Create a plugin to run Python Doctest in Vim (2)
How to write a list / dictionary type of Python3
Create a plugin to run Python Doctest in Vim (1)
Things to note when initializing a list in Python
[Python] I want to make a nested list a tuple
Python script to create a JSON file from a CSV file
[Python] How to create a 2D histogram with Matplotlib
How to create a kubernetes pod from python code
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
[Python] Convert list to Pandas [Pandas]
A road to intermediate Python
[Python] How to use list 1
Create ToDo List [Python Django]
Python list is not a list
Create a python numpy array
[Introduction to Python] <list> [edit: 2020/02/22]
Create a directory with python
[Python] How to make a list of character strings character by character
Create a list in Python with all followers on twitter
How to shuffle a part of a Python list (at random.shuffle)
Create a Mastodon bot with a function to automatically reply with Python
Probably the easiest way to create a pdf with Python3
[Python] Create a date and time list for a specified period
Various ways to calculate the similarity between data in python
Developed a library to get Kindle collection list in Python
[Python Kivy] How to create a simple pop up window
[Road to intermediate Python] Use if statement in list comprehension
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Create a tool to check scraping rules (robots.txt) in Python
Python: Create a dictionary from a list of keys and values
Create a message corresponding to localization with python translation string
Extract the value closest to a value from a Python list element
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
How to write a Python class
[python] Manage functions in a list
Create a python GUI using tkinter