[Python] How to use the enumerate function (extract the index number and element)

How to use python's enumerate function (extract index number and element)

Check how to use the enumerate function by dividing it into patterns.


**table of contents**
  1. [What is the enumerate function](# 1-What is the enumerate function)
  2. [What you can do](#You can do two things)
  3. [How to use](# How to use)
  4. [Fetch index number and element (1 variable)](# 2-1 variable to retrieve index number and element) )
  5. [Retrieve index number and element separately](# 3-Extract index number and element separately)
  6. [Retrieve only index number](# 1-Extract only index number)
  7. [Extract only elements](# 2-Extract only elements)
  8. [Remove both](# 3-Remove both)
  9. [Change the initial value of the index number](# 4-Change the initial value of the index number)
  10. [Use for tuple](# 5-Use for tuple)

## 1. What is the enumerate function? ### 3 things you can do

-Retrieve the index number of the array -Set the initial value of the index number arbitrarily ・ Extract elements

You can retrieve the element without enumerate, so use it when you want the index number.

How to use

for a, b in enumerate(A, n): └ "a": Variable to put the index number └ "b": Variable to put the element └ "A": array (or tuple) └ "n": Initial value of index number

The print method is often used to see the output.

** ▼ Point ** ・ Used as a set with a for statement └ Not used by enumerate alone

-Define two variables └ Variable to put index number └ Variable to put the element

・ Even one variable is OK └ Index number and element are a set └ Output: (index number, element)

・ The initial value of the index number can be set arbitrarily. └ Default starts from 0 └ Only integers (minus can be specified)


## Example (by pattern) ### 2. Extract the index number and element (1 variable) `for a in enumerate(A): ` └ Specify only one variable (a in this case)

▼ Output (Index number, element)

1 variable


list = ["AAA","BBB","CCC","DDD"]

for a in enumerate(list):
    print(a)

#Output result
(0, 'AAA')
(1, 'BBB')
(2, 'CCC')
(3, 'DDD')

Of course, the variables are arbitrary. Either i or AAA is OK.


## 3. Extract the index number and element separately

1. Extract only the index number

for a,b in enumerate(A): └ "a": Variable that contains the index number └ "b": Variable that contains the element └ "A": array (or tuple)

Only the variable defined earlier (here, "a") is used at the time of output.

Extract only index number(2 variables)


list = ["AAA","BBB","CCC","DDD"]

for a,b in enumerate(list):
    print(a)

#Output result
0
1
2
3

▼ It can also be obtained by specifying the 0th element with one variable.

・ A = (index number, element) ・ A [0] = index number

Extract only index number(1 variable)


list = ["AAA","BBB","CCC","DDD"]

for a in enumerate(list):
    print(a[0])

#Output result
0
1
2
3

### 2. Extract only elements

for a,b in enumerate(A): └ "a": Variable that contains the index number └ "b": Variable that contains the element └ "A": array (or tuple)

Only the variable defined later (here, "b") is used at the time of output.

Extract only index number(2 variables)


list = ["AAA","BBB","CCC","DDD"]

for a,b in enumerate(list):
    print(b)

#Output result
AAA
BBB
CCC
DDD

▼ You can also get it by specifying the first element with one variable.

・ A = (index number, element) ・ A [1] = index number

Extract only index number(1 variable)


list = ["AAA","BBB","CCC","DDD"]

for a in enumerate(list):
    print(a[1])

#Output result
AAA
BBB
CCC
DDD

### 3. Take out both `for a,b in enumerate(A): ` └ "a": Variable that contains the index number └ "b": Variable that contains the element └ "A": array (or tuple)

Both variables defined at the time of output (here, "a" and "b") are used.

Extract index number and element separately


list = ["AAA","BBB","CCC","DDD"]

for a,b in enumerate(list):
    print(a,b)

#Output result
0 AAA
1 BBB
2 CCC
3 DDD

Output example using format method


list = ["AAA","BBB","CCC","DDD"]

for a,b in enumerate(list):
    print("index number={:^4}element={:^8}".format(a,b))

#Output result
index number=0 element=  AAA   
index number=1 element=  BBB   
index number=2 elements=  CCC   
index number=3 elements=  DDD  

How to use the format method here


## 4. Change the initial value of the index number

for a, b in enumerate(A, n): └ "a": Variable to put the index number └ "b": Variable to put the element └ "A": array (or tuple) └ "n": Initial value of index number (integer)

Any integer entered in "n" will be the initial value.

1 variable


list = ["AAA","BBB","CCC","DDD"]

for a in enumerate(list, 999):
    print(a)


#Output result
(999, 'AAA')
(1000, 'BBB')
(1001, 'CCC')
(1002, 'DDD')

2 variables


list = ["AAA","BBB","CCC","DDD"]

for a, b in enumerate(list, 999):
    print(a)


#Output result
999
1000
1001
1002

Minus can also be specified


list = ["AAA","BBB","CCC","DDD"]

for a, b in enumerate(list, -2):
    print(a)

#Output result
-2
-1
0
1

Fraction (float) is an error


list = ["AAA","BBB","CCC","DDD"]

for a, b in enumerate(list, 999.9):
    print(a)


#Output result
# TypeError: 'float' object cannot be interpreted as an integer

## 5. Use for tuples

It is commonly used for lists, but it can also be used for tuples.

Click here for a description of tuples

Used for tuple


tuple = "AAA","BBB","CCC","DDD"
type(tuple)
#Output: tuple

for a in enumerate(tuple):
    print(a)

#Output result
(0, 'AAA')
(1, 'BBB')
(2, 'CCC')
(3, 'DDD')

[Back to top](How to use #python's enumerate function Extract index number and element )

Recommended Posts

[Python] How to use the enumerate function (extract the index number and element)
How to use Python zip and enumerate
[Python] How to use hash function and tuple.
How to use the zip function
How to use python zip function
[python] How to use __command__, function explanation
python: How to use locals () and globals ()
How to use is and == in Python
[Python] Explains how to use the format function with an example
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to use the C library in Python
[Python] Explains how to use the range function with a concrete example
[Algorithm x Python] How to use the list
I want to get the file name, line number, and function name in Python 3.4
[Python] [Django] How to use ChoiceField and how to add options
How to get the number of digits in Python
[Introduction to Python] How to iterate with the range function?
How to use the Raspberry Pi relay module Python
[Python] How to use the graph creation library Altair
How to use the grep command and frequent samples
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use argparse and the difference between optparse
How to use the model learned in Lobe in Python
How to use the generator
[Python] How to use list 1
About the enumerate function (python)
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to identify the element with the smallest number of characters in a Python list?
How to count the number of occurrences of each element in the list in Python with weight
[python] How to use the library Matplotlib for drawing graphs
How to use the __call__ method in a Python class
[Hyperledger Iroha] Notes on how to use the Python SDK
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
I didn't know how to use the [python] for statement
I tried to enumerate the differences between java and python
[Introduction to Python] How to get data with the listdir function
Extract the value closest to a value from a Python list element
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Python: How to use async with
How to use the optparse module
How to install and use Tesseract-OCR
How to use Requests (Python Library)
How to use SQLite in Python
How to get the Python version
[Python] How to import the library
How to use .bash_profile and .bashrc
How to install and use Graphviz
[Python] How to use list 3 Added
How to use Mysql in python