Built-in Python function that makes an iterator that aggregates elements from each of the iterables.
Questions tagged [python-zip]
58 questions
188
votes
9 answers
Zip lists in Python
I am trying to learn how to "zip" lists. To this end, I have a program, where at a particular point, I do the following:
x1, x2, x3 = stuff.calculations(withdataa)
This gives me three lists, x1, x2, and x3, each of, say, size 20.
Now, I do:
zipall…

AJW
- 5,569
- 10
- 44
- 57
28
votes
4 answers
How to combine slices into a slice of tuples in Go (implementing python `zip` function)?
Sometimes, it's convenient to combine two lists into a tuple using zip built-in function in Python. How to make this similarly in Go?
For example:
>>> zip ([1,2],[3,4])
[(1,3), (2,4)]

holys
- 13,869
- 15
- 45
- 50
18
votes
1 answer
TypeError: zip argument #1 must support iteration
for k,v in targets.iteritems():
price= str(v['stockprice'])
Bids = str(u''.join(v['OtherBids']))
Bids = Bids.split(',')
# create a list of unique bids by ranking
for a, b in zip(float(price), Bids):
try:
del…

Ting Ping
- 1,145
- 7
- 18
- 34
8
votes
2 answers
Python iterator and zip
With x = [1,2,3,4], I can get an iterator from i = iter(x).
With this iterator, I can use zip function to create a tuple with two items.
>>> i = iter(x)
>>> zip(i,i)
[(1, 2), (3, 4)]
Even I can use this syntax to get the same results.
>>> zip(*[i]…

prosseek
- 182,215
- 215
- 566
- 871
5
votes
2 answers
Printing an unzipped list object returns empty list
In the following code, I am trying to unzip a zip object.
x = [1, 2, 3]; y = ['a', 'b', 'c']
z = zip(x, y)
#print(list(z)) #2nd print statement returns [] if this line is uncommented
unzip = zip(*z)
print(list(unzip)) …

Perspicacious
- 594
- 3
- 17
4
votes
1 answer
list-comprehension throws a RuntimeError
Why does this code work well and does not throw exceptions?
def myzip(*args):
iters = [iter(arg) for arg in args]
try:
while True:
yield tuple([next(it) for it in iters])
except StopIteration:
return
for x,…

Артём Гаркавый
- 383
- 2
- 10
3
votes
3 answers
Sum values of each tuples enclosed of two lists. The problem is that they add together, but don't sum
I would sum the values of each tuple enclosed in two lists. The output i would like to get is: 125, 200.0, 100.0.
The problem is that they don't sum, but they add like this [(87.5, 37.5), (125.0, 75.0), (50.0, 50.0)]. I need first and second to stay…
user20812299
3
votes
4 answers
how to flatten 2D list and add a separator character using zip and list comprehension in python?
I'm trying to write a statement in python that turns this input (for example):
[[3,4],[1],[1,2]]
into this output:
[3,4,-,1,-,1,2]
using only zip and list comprehension
this is my code:
a = [[1,2],[1],[3,4]]
result = [j for i in zip(a,'-'*len(a))…

Alex Butane
- 127
- 8
3
votes
1 answer
Unpacking a zip object causing ValueError
I created a zip object using the following line of code:
k=zip([1,2,3],['a','b','c'])
Converting this into a list gives the output:
[(1,'a'),(2,'b'),(3,'c')]
However, when I use this line of code
x,y=zip(*k)
it gives me this…

random math student
- 77
- 7
2
votes
2 answers
Concentrate two lists alternating python
I want to combine two lists in an alternating way in Python.
list1 = ['A']
list2 = [1, 2, 3, 4]
What I've tried:
combination = [x for y in zip(list1, list2) for x in y]
# Output:
['A', 1]
# Expected output:
['A', 1, 'A', 2, 'A', 3, 'A', 4]
How…
user21060017
2
votes
2 answers
combining elements of 2 list in a certain way
i have a doubt in combining 2 lists into one in a certain way.
can anyone help me out?
i have 2 lists
a = ['a', 'b', 'c', 'd', 'e']
b = [1, 2, 3]
i want the resultant list like ['a1', 'b2', 'c3', 'd1', 'e2']
The result list must be generated by…

Rohith Mohanan
- 21
- 2
2
votes
2 answers
Python - Why does enumerate() cause a later zip() to only pull from the last item in list?
Code:
boylist = ['Jim', 'James', 'Jack', 'John', 'Jason']
for i, boylist in enumerate(boylist):
print(f'Index {i} is {boylist} in my list')
#boylist = ['Jim', 'James', 'Jack', 'John', 'Jason']
girllist = ['Emma', 'Clara', 'Susan', 'Jill',…

Fake Fakerson
- 23
- 2
2
votes
2 answers
Fix filenames with encoding when unzipping with special characters in python
There are many questions about encoding our there but I still have not been able to solve my problem.
Imagine I have three files within a compressed ZIP file:
Übersicht.pdf
finalePräsentation
münchen
I want to unzip those files so I do:
with…

shadow
- 151
- 2
- 12
2
votes
2 answers
How to compare elements in list-of-lists?
I have a list which contains list of elements(the number of elements in each inner list are not same) and I want to group all the elements in same index into separate groups and return maximum values in each group:
for example,
elements = [[89, 213,…

Shashiwadana
- 492
- 1
- 8
- 16
2
votes
0 answers
How can I add a progress bar on Zip extracting in Python?
I have a large dataset in Google Drive. I'm using Google Colab. I extracted the dataset in a cell of colab using python.
But I have to wait till it finished.
Is there any way to add a progress bar to this Code in down below?
So that it shows the…

Azmir Murad
- 21
- 2