About the enumerate function (python)

1. Introduction

Hello! !! This time I would like to write an article about the enumerate function of python. You can use the enumerate function to get the elements and order of a list or tuple in a for loop. In short, it is a function that returns the contents of lists and tuples and their order numbers. You can use the enumerate function to simplify your code and make it intuitive. Now, let's solve a simple problem with and without the enumerate function.

2. Problem

There is a regular presentation in the laboratory. I would like to go to the order of presentations in the order of this prepared list (array). However, since the order is not written in this list, it is not intuitive. So please output the name and the order of presentation.

Let's solve such a problem.

3. When not handling the enumerate function

If you don't handle the enumerate function, you'll get code like this:

enumerate.py


publicator = ["haruto","sota","minato","yuto","riku","mei"]
num = len(publicator)

for i in range(num):
    print(i+1,publicator[i])
1 haruto
2 sota
3 minato
4 yuto
5 riku
6 mei

It handles the len function, gets the number of arrays, and does a for loop.

4. When dealing with the enumerate function

Then, when dealing with the enumerate function of the main subject, the code looks like this.

enumerate.py


publicator = ["haruto","sota","minato","yuto","riku","mei"]

for i,pub in enumerate(publicator,1):
    print(i,pub)
1 haruto
2 sota
3 minato
4 yuto
5 riku
6 mei

You don't have to use two variables, and I think it's easier to code when dealing with the enumerate function. By the way, the part of enumerate (publicator, 1) means that the order number of the array publicator starts from 1, so it is a convenient function because it does not have to be i + 1.

5 Finally

The enumerate function was handled in a for loop and returned the elements of lists and tuples and their order numbers. It's surprisingly convenient to write code.

In the second post of Qiita, it may be difficult to understand because I have never sent information such as blogs. From now on, I would like to disseminate information and improve my writing skills, and my dream is to become a person who can play with technology, so please watch with warm eyes. Thank you in advance. Well then!

Recommended Posts

About the enumerate function (python)
python enumerate function
About function arguments (python)
About the Unfold function
Python: About function arguments
About the Python module venv
About python beginner's memorandum function
About the ease of Python
About the features of Python
python function ①
[Python] function
python function ②
[Python] What is @? (About the decorator)
[Python] Make the function a lambda function
About the basics list of Python basics
Roughly think about the loss function
About python comprehension
[Python Kivy] About changing the design theme
About the test
About Python tqdm.
About python yield
About the virtual environment of python version 3.7
OR the List in Python (zip function)
Python> function> Closure
[Python] Generator function
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Python3] Rewrite the code object of the function
About python inheritance
[Python] Summarize the rudimentary things about multithreading
About python, range ()
About the arguments of the setup function of PyCaret
About python decorators
Python> function> Inner function
About python reference
About Python decorators
[Python] About multi-process
Python function decorator
About the queue
[Python] How to use the enumerate function (extract the index number and element)
[Python] I tried substituting the function name for the function name
To execute a Python enumerate function in JavaScript
A Java programmer studied Python. (About the decorator)
Get the caller of a function in Python
About the difference between "==" and "is" in python
A note about the python version of python virtualenv
A memorandum about the Python tesseract wrapper library
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
I implemented the inverse gamma function in python
About the * (asterisk) argument of python (and itertools.starmap)
[Python] Seriously think about the M-1 winning method.
About Python for loops
[Python] Operation of enumerate
Summary about Python scraping
Function execution time (Python)
[Python] Memo about functions
Summary about Python3 + OpenCV3
the zen of Python
About Python, for ~ (range)
Python function argument summary
The first GOLD "Function"