How to use Python zip and enumerate

I found an article on python2.X and confirmed that it works on python3.X, so I wrote it as a memorandum.

Preparation

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

reference

https://www.saltycrane.com/blog/2008/04/how-to-use-pythons-enumerate-and-zip-to/

Recommended Posts

How to use Python zip and enumerate
How to use python zip function
How to install and use pandas_datareader [Python]
How to use is and == in Python
python3: How to use bottle (2)
How to use Python argparse
[Python] How to use checkio
[Python] How to use hash function and tuple.
[Python] How to use input ()
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
[Python] How to use the enumerate function (extract the index number and element)
How to use the zip function
How to install and use Tesseract-OCR
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
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
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
[Python] How to use Typetalk API
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Summary of how to use pandas
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to use __slots__ in Python class
How to use "deque" for Python data
[Python] How to calculate MAE and RMSE
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
[Blender x Python] How to use modifiers
How to use pandas Timestamp and date_range
[Question] How to use plot_surface of python
How to use Serverless Framework & Python environment variables and manage stages
How to use Python with Jw_cad (Part 2 Command explanation and operation)
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python