Python basic operation 1st: List comprehension notation

#Python basic operation

Introduction

Review the basic operations of Python only for the parts that you are uneasy about. As the first step, we describe list comprehension notation.

Why do we need list comprehensions?

・ Speeding up processing ・ Simplification of structure Mainly because these benefits may be obtained. Here, the description method and evaluation will be performed.

Description method 1

List the variables that store the processing contents and their processing in parentheses []

[Variable processing] Example) data = [i for i in range (10)]

Description method 2

List the variables that store the processing contents and their processing and execution conditions in parentheses [].

[Variable processing conditions] Example) data = [i for i in range (10) if i%2 == 0]

Description method 3

When processing is required when the condition is not satisfied, such as if ~ else ..., the conditions are arranged in between.

[Variable condition processing] Example) data = [i if i%2 == 0 else 0 for i in range (10)]

code

List_Comprehension.py


"""
2020/12/13
@Yuya Shimizu

List comprehension
"""
#Description method 1:[Variable processing]
data = [i for i in range(10)]
print(data)

#Description method 2:[Variable processing conditions]
data = [x for x in range(10) if x%2 == 0]
print(data)

#Description method 3:[Variable condition processing]
data = [y if y%2 == 0 else "xxx" for y in range(10)]
print(data)

Is it really faster?

Certainly, I think that the structure can be simplified with one line. But is it really faster? In the following, the verification is performed using the time module. Since it was difficult to understand the difference when the number of repetitions was small, It was verified under 5 conditions of repetition number 1000, 2000, 3000, 4000, 5000.

code

List_Comprehension.py


"""
2020/12/13
@Yuya Shimizu

List comprehension
"""
iteration = 1000 #Repeat condition{1000,2000,3000,4000,5000}
def func1():
    #[Variable processing]
    data = []
    for i in range(iteration):
        data.append(i)
    print(data)

    #[Variable processing conditions]
    data = []
    for x in range(iteration):
        if x%2 == 0:
            data.append(x)
    print(data)

    #[Variable condition processing]
    data = []
    for y in range(iteration):
        if y%2 == 0:
            data.append(y)
        else:
            data.append("xxx")
    print(data)

def func2():
    #[Variable processing]
    data = [i for i in range(iteration)]
    print(data)

    #[Variable processing conditions]
    data = [x for x in range(iteration) if x%2 == 0]
    print(data)

    #[Variable condition processing]
    data = [y if y%2 == 0 else "xxx" for y in range(iteration)]
    print(data)


######Execution time measurement
import time

start1 = time.time()
func1()
process_time1 = time.time() - start1

start2 = time.time()
func2()
process_time2 = time.time() - start2

print("List comprehension(iteration={})----None:{}Seconds, Yes:{}Seconds".format(iteration, process_time1, process_time2))

inspection result

Number of repetitions[Times] Execution time without list comprehension[s] Execution time with list comprehension[s]
1000 1.139344 1.121223
2000 1.026536 0.578534
3000 0.856933 0.884612
4000 0.766533 0.579598
5000 0.667517 0.605027

Speeding up is certainly seen at 2000 and 4000 times, but it is not always possible. It can be seen that the speed may be increased. In fact, if you look closely, the result is that when the number of trials is 3000, it is rather slower to use the list comprehension notation.

Impressions

According to the verification results, it is not always possible to increase the speed, but if there is a possibility and the delay is slight, I wondered if it should be utilized. Besides, I think that the simplification of the structure is convenient for debugging. If there are situations where it can be used, I would definitely like to use list comprehension notation.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

Python basic operation 1st: List comprehension notation
Python> Comprehension / Comprehension> List comprehension
Python Exercise 2 --List Comprehension
Python list comprehension speed
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
Basic operation list of Python3 list, tuple, dictionary, set
Python basic course (10 inclusion notation)
Python Basic Course (5 List Tuples)
Basic Python operation 2nd: Function (argument)
Python comprehension
List comprehension
Comprehension notation
Python comprehension
List comprehension
[Python] list
Comprehension notation
Python comprehension (list and generator expressions) [additional]
[Introduction to Udemy Python3 + Application] 17. List operation
Python basic operation 3rd: Object-oriented and class
Basic grammar of Python3 series (list, tuple)
Basic grammar of Python3 system (included notation)
[Python] List Comprehension Various ways to create a list
About python comprehension
Python basics: list
RF Python Basic_01
[python] vector operation
Basic Python writing
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
Python OS operation
Python3 basic grammar
[Python] Matrix operation
RF Python Basic_02
Note: List comprehension
Python list manipulation
List reverse operation
Basic operation of Python Pandas Series and Dataframe (1)
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
Let's do various things using Python's list comprehension notation
list comprehension because operator.methodcaller cannot be used in python 2.5
Installing Python 3 on Mac and checking basic operation Part 1
Python3 comprehension (List, dictionary) that I have seen somewhere
Sorted list in Python
[Python] Operation of enumerate
Python> list> extend () or + =
Python basic course (12 functions)
Python I'm also basic
Python basic grammar / algorithm
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
FizzBuzz in list comprehension
Basic sorting in Python
Python basic course (9 iterations)
Basic operation of pandas
Filter List in Python
[python] class basic methods
python unittest assertXXX list
Python Basic Course (11 exceptions)
Generator comprehension notation Tuple comprehension notation
Python basic course (6 sets)
Basic operation of Pandas
Python3 cheat sheet (basic)