How to use python zip function

This is Qiita's first post. I would appreciate it if you would kindly watch over me. I decided to work with python in my university research, and I decided to output it in case I forgot what I had stumbled upon or learned because I had never touched programming. I hope you can help someone.

Why use the python zip function?

After learning python, the zip function came out, but when I first saw it, it was "What is this". When I look it up

"The zip function is a function to use when you want to get multiple lists at the same time." Https://www.sejuku.net/blog/66268

Hmmmm ... it's difficult for me. For the time being, can I get multiple lists at the same time ... but I don't know when to actually use it and what to do! That's why I've been thinking about the good points of the zip function. I think that the zip function can simplify the code, so I would like to write about the case where the zip function is used and the case where it is not used for the problem that I actually want to execute.

problem

I want to know each person's name and gender and where they live at once.

Do not use the zip function.

If you try to write code without using the zip function, it will look like this.

no_zip.py


name = ['Noah','Emma','James']
gender = ['men','woman','men']
address = ['tokyo','chiba','nagoya']

for i in range(len(name)):
    print(name[i],gender[i],address[i])

The output is

Noah men tokyo
Emma woman chiba
James men nagoya

It will be. First, create an array and rotate (0 to 2) as many times as the number of variables name in the for statement. Therefore, other variables gender and address also output the corresponding elements.

Use the zip function.

Next is the case when dealing with the zip function.

yes_zip.py


name = ['Noah','Emma','James']
gender = ['men','woman','men']
address = ['tokyo','chiba','nagoya']

for n,g,a in zip(name,gender,address):
    print(n,g,a)

The output is

Noah men tokyo
Emma woman chiba
James men nagoya

It will be. I think the strange part is the for statement. In the name of the zip function, the first'Noah' in the array is picked up and put in n, and in the gebder, the first'men' is picked up and put in g. That is, the zip function can "get the index of more than one list".

I think it is the zip function that is intuitively easy to understand by looking at the code.

Finally

This is Qiita's first post, and 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.

Recommended Posts

How to use python zip function
How to use the zip function
How to use Python zip and enumerate
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[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 hash function and tuple.
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
Python: How to use pydub (playback)
How to use PubChem in Python
[Python] How to use Typetalk API
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
[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
[Introduction to Udemy Python3 + Application] 46. Zip function
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
[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
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to install python
How to use partial
How to use Bio.Phylo
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib