In this article, I'd like to talk about the Python built-in function "zip function". I've known the existence of the "zip function" before, and I've been using it for some reason, so I'd like to deepen my understanding of the "zip function" in this period.
What is a "zip function" in the first place? Simply put, it is a collection of multiple lists given as arguments. If you want to know more, please click here. Python standard library
As a basic usage, the one used in the for statement can be mentioned.
#Create a list of names and ages
names = ['sato', 'suzuki', 'ito']
ages = [23, 21, 25]
for name, age in zip(names, ages):
print('name:{},age:{}'.format(name, age))
#name:sato,age:23
#name:suzuki,age:21
#name:ito,age:25
You can put the two lists together in this way.
It is also possible with three lists.
#Create a list of names, ages and hometowns
names = ['sato', 'suzuki', 'ito']
ages = [23, 21, 25]
birthplaces = ['Tokyo', 'Aichi prefecture', 'Hokkaido']
for name, age, birthplace in zip(names, ages, birthplaces):
print('name:{},age:{},Birthplace:{}'.format(name, age, birthplace))
#name:sato,age:23,Birthplace:Tokyo
#name:suzuki,age:21,Birthplace:Aichi prefecture
#name:ito,age:25,Birthplace:Hokkaido
You will find it useful when you want to put together a list.
You can make it even shorter if you just put it together.
list(zip(names, ages))
#[('sato', '23'), ('suzuki', '21'), ('ito', '25')]
This will return it as a tuple.
How can I get the zip back together? The answer can be given by adding "*" to the argument.
zips = list(zip(names, ages))
names2, ages2 = zip(*zips)
print(names2)
#('sato', 'suzuki', 'ito')
print(ages2)
#(23, 21, 25)
It will be returned as a tuple like this. If you want to output as a list, enclose it in "list ()".
zips = list(zip(names, ages))
names2, ages2 = list(zip(*zips))
print(names2)
#[('sato', 'suzuki', 'ito')]
print(ages2)
#[(23, 21, 25)]
Let's see what happens when the list lengths are different.
#Create a list of names and ages
names = ['sato', 'suzuki', 'ito', 'kato', 'saitou']
ages = [23, 21, 25]
for name, age in zip(names, ages):
print('name:{},age:{}'.format(name, age))
#name:sato,age:23
#name:suzuki,age:21
#name:ito,age:25
If you look at this, you can see that it is suitable for the shorter one.
Earlier, when the length of the list was different, I think I was able to confirm that it fits the shorter one. So how do you fit the longer one? You can do that by using the "zip_longest module". Please see here for details. zip_longest module
Let's see.
#Create a list of names and ages
names = ['sato', 'suzuki', 'ito', 'kato', 'saitou']
ages = [23, 21, 25]
for name, age in zip_longest(names, ages):
print('name:{},age:{}'.format(name, age))
#name:sato,age:23
#name:suzuki,age:21
#name:ito,age:25
#name:kato,age:None
#name:saitou,age:None
You can see that it is filled with "None" like this. "None" is dull, so let's enter the average age instead.
#zip_Import longest
from itertools import zip_longest
#Create a list of names and ages
names = ['sato', 'suzuki', 'ito', 'kato', 'saitou']
ages = [23, 21, 25]
#Calculate average age
age_mean = sum(ages) // len(ages)
#Specify the value to be filled with fillvalue
for name, age in zip_longest(names, ages, fillvalue=age_mean):
print('name:{},age:{}'.format(name, age))
#name:sato,age:23
#name:suzuki,age:21
#name:ito,age:25
#name:kato,age:23
#name:saitou,age:23
You can see that it is filled with the average age.
Finally, let's check when the lengths of three or more lists are different.
#zip_Import longest
from itertools import zip_longest
#Create a list of names, ages and hometowns
names = ['sato', 'suzuki', 'ito']
ages = [23, 21, 25]
birthplaces = ['Tokyo', 'Aichi prefecture', 'Hokkaido', 'Yamanashi Prefecture']
#Calculate average age
age_mean = sum(ages) // len(ages)
#Specify the value to be filled with fillvalue
for name, age, birthplace in zip_longest(names, ages, birthplaces, fillvalue=age_mean):
print('name:{},age:{},Birthplace:{}'.format(name, age, birthplace))
#name:sato,age:23,Birthplace:Tokyo
#name:suzuki,age:21,Birthplace:Aichi prefecture
#name:ito,age:25,Birthplace:Hokkaido
#name:kato,age:23,Birthplace:Yamanashi Prefecture
#name:saitou,age:23,Birthplace:23
When you check the output result, the birthplace of the last line is "23". Please note that you can only fill one value in this way.
Please refer to this article for countermeasures in that case. How to use Python and zip functions: Get multiple list elements at once
This time, I covered the "zip function". We look forward to helping you. We will continue to publish articles about functions and modules, so please take a look if you like.
Recommended Posts