Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip() in python, how to use static values

I'm trying to build an array of tuples with first value from list with some static values.

It should be simple but I'm struggling to do this for some reason.

For example, how do I get the following:

 [(1,100,200),
  (2,100,200),
  (3,100,200),
  (4,100,200),
  (5,100,200)]

>>> zip([1,2,3,4,5],100,200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip argument #2 must support iteration
like image 303
mikkom Avatar asked Oct 23 '25 17:10

mikkom


1 Answers

You could use itertools.repeat to repeat the elements that you're trying to zip together.

>>> import itertools
>>> zip([1, 2, 3, 4, 5], itertools.repeat(100), itertools.repeat(200))
[(1, 100, 200), (2, 100, 200), (3, 100, 200), (4, 100, 200), (5, 100, 200)]

You could also specify the number of times you need to repeat the element. (5 in this case)

like image 77
Sukrit Kalra Avatar answered Oct 25 '25 06:10

Sukrit Kalra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!