python memo: enumerate () -get index and element of list at the same time and turn for statement

Motivation

[Previous article] Continuing from (), I was reading "Detailed explanation of time-series data processing by deep learning Tensorflfow / Keras". When defining the model structure in, a function called enumerate () was used in the for statement, but I didn't know the contents, so I looked it up.

enumerate (): Get list indexes and elements at once

I used this as a reference. http://www.gesource.jp/programming/python/code/0022.html

enumerate_check.py


a = [(i+1)*10 for i in range(10)]

for index, element in enumerate(a):
  print(index, '-', element)

Execution result 0 - 10 1 - 20 2 - 30 3 - 40 4 - 50 5 - 60 6 - 70 7 - 80 8 - 90 9 - 100

When will it be useful?

enumerate_check2.py


a = [(i+1)*10 for i in range(10)]

for i in range(len(a)):
  print(i, '-', a[i])

This was the same result. Is enumerate () better? Is it just a readability issue?

Recommended Posts

python memo: enumerate () -get index and element of list at the same time and turn for statement
python note: map -do the same for each element of the list
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
[Introduction to Python] How to get the index of data with a for statement
Get the number of occurrences for each element in the list
Get the index of each element of the confusion matrix in Python
I tried to get the index of the list using the enumerate function
[Python] How to use the enumerate function (extract the index number and element)
Get a datetime instance at any time of the day in Python
I measured the speed of list comprehension, for and while with python2.7.
Count the number of times two values appear in a Python 3 iterator type element at the same time
At the time of python update on ubuntu
Get the update date of the Python memo file.
[Python] Create a list of date and time (datetime type) for a certain period
Python script to get a list of input examples for the AtCoder contest
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python
List of Python libraries for data scientists and data engineers
[Python] Measures and displays the time required for processing
[python] Get the list of classes defined in the module
Visualize data and understand correlation at the same time
Python: Get a list of methods for an object
Check the processing time and the number of calls for each process in python (cProfile)
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
Get the value of a specific key up to the specified index in the dictionary list in Python
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Try to get the function list of Python> os package
wxPython: Draw animation and graph drawing at the same time
[Python] Create a date and time list for a specified period
Get the number of specific elements in a python list
Get index of nth largest / smallest value in list in Python
Get the current date and time in Python, considering the time difference
Turn an array of strings with a for statement (Python3)
Get index of nth largest / smallest value in list in Python
[Python / PyQ] 4. list, for statement
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
The story of returning to the front line for the first time in 5 years and refactoring Python Django
Note: Get the first and last items of Python OrderedDict non-destructively
[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]
[Python] How to get the first and last days of the month
Get and set the value of the dropdown menu using Python and Selenium
Browse .loc and .iloc at the same time in pandas DataFrame
Get the title and delivery date of Yahoo! News in Python
The story of Python and the story of NaN
Get the first element of queryset
See python for the first time
About the basics list of Python basics
For Windows: Get a list of directories and files under a specific directory.
[Python] Visualize the heat of Tokyo and XX prefectures (DataFrame usage memo)
How to get the date and time difference in seconds with python
Python> sys.path> List of strings indicating the path to search for modules
[Python] Case where the execution time differs between the built-in list and deque
Python beginner ~ Round off the Nth decimal place and output ~ (for memo)
I measured 6 methods to get the index of the maximum value (minimum value) of the list
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)
Effective Python memo Item 7 Use list comprehension instead of map and filter
Get and convert the current time in the system local timezone with python
Get the number of articles accessed and likes with Qiita API + Python
Get the list of packages for the specified user from the packages registered on PyPI
Get and estimate the shape of the head using Dlib and OpenCV with python