Effective Python Memo Item 11 Use zip to process iterators in parallel

This is a memo of O'Reilly Japan's book effective python. https://www.oreilly.co.jp/books/9784873117560/ P21~23

Consider the case of processing multiple list elements in parallel

** Extract the name with the longest number of characters **

names = ['Cecilia', 'Lise', 'Marie' ]
letters = [len(n) for n in names]

longest_name = None
max_letters = 0

#Pattern 1
for i in range(len(names)):
    count = letters[i]
    if count > max_letters:
        longest_name = names[i]
        max_letters = count

print(longest_name)

>>>
Cecilia

There is no syntactic problem, but it is hard to see that it is accessed twice with the subscript of the list. Also, if the list is extremely long, it is not ecological to expand all the contents, so I would like to solve it with a generator.

for name, count in zip(names, letters): #Note the size of the zip element
    if count > max_letters:
        longest_name = name
        maxx_letters = count
        
print(longest_name)

>>>
Cecilia

If you use zip (), the list of contents is returned in parallel by the generator, so it is a memory-friendly specification. Also, it is easier to see than anything else.

However, if the elements in the zip do not match. Note that the whole thing will end when the shorter iterator runs out.

Recommended Posts

Effective Python Memo Item 11 Use zip to process iterators in parallel
EP 11 Use `zip` to Process Iterators in Parallel
Effective Python Memo Item 3
Effective Python Memo Item 18 Use variable-length positional arguments to make the appearance cleaner
[R] [Python] Memo to read multiple csv files in multiple zip files
Effective Python Memo Item 19 Give optional behavior to keyword arguments
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use python zip function
[Introduction to Python] How to use class in Python?
Effective Python memo Item 10 Enumerate from range
Easy way to use Wikipedia in Python
How to use __slots__ in Python class
How to use Python zip and enumerate
How to use regular expressions in Python
How to use is and == in Python
Effective Python memo Item 7 Use list comprehension instead of map and filter
Effective Python Memo Item 8 Avoid three or more expressions in list comprehensions
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
Use cryptography module to handle OpenSSL in Python
How to use tkinter with python in pyenv
Use os.getenv to get environment variables in Python
[For beginners] How to use say command in python!
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Parallel download in Python
Convert the image in .zip to PDF with Python
I tried to summarize how to use pandas in python
[Python] Retry process (Exponential Backoff) memo in AWS Lambda
Use profiler in Python
How to use the model learned in Lobe in Python
I want to use the R dataset in python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
Output "Draw ferns programmatically" to the drawing process in Python
Effective Python Memo Item 9 Consider generator expressions for large comprehensions
How to use the __call__ method in a Python class
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Road to intermediate Python] Use if statement in list comprehension
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
Let's use def in python
Use let expression in Python
Use Measurement Protocol in Python
python3: How to use bottle (2)
Use callback function in Python
Use parameter store in Python
[Python] How to use list 1
Run Python unittests in parallel
Login to website in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
How to use Python argparse
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE