I found an article on python2.X and confirmed that it works on python3.X, so I wrote it as a memorandum.
list.py
a = ['Ah','I','U','e','O']
b = ['Or','Ki','Ku','Ke','This']
For example, suppose you have a list like this.
enumerate
enumerate.py
for i,ai in enumerate(a):
print(i,ai)
Then
Execution result
0 Oh
1
2
3 Eh
4
It looks like this
zip
zip.py
for ai,bi in zip(a,b):
print(ai,bi)
Then
Execution result
Red
breath
float
Eke
This
Like this. By the way, three or more lists can be put together in the same way.
enumerate & zip When you want to use enumerate and zip at the same time
error.py
for i,ai,bi in enumerate(zip(a,b)):
print(i,ai,bi)
Writing like this caused an error.
Execution result
ValueError: not enough values to unpack (expected 3, got 2)
So, as a result of investigating whether there is any method, it seems that it should be done as follows.
success.py
for i,(ai,bi) in enumerate(zip(a,b)): #At the zip()Surrounded by
print(i,ai,bi)
Execution result
0 red
1
2
3
4
https://www.saltycrane.com/blog/2008/04/how-to-use-pythons-enumerate-and-zip-to/
Recommended Posts