Does the zip function work with lists of different lengths?

Question

Do the lists passed to the `zip()` function have to be the same length?

The `zip()` function will only iterate over the smallest list passed. If given lists of different lengths, the resulting combination will only be as long as the smallest list passed. In the following code example, `list_two` contains more elements than `list_one` so the resulting merged list will only be as long as `list_one`.

``````list_one = ['Joe', 'Mark', 'Jane']
list_two = [ 100, 34, 87, 23, 65 ]
merged = zip(list_one, list_two)
print(list(merged))
# [('Joe', 100), ('Mark', 34), ('Jane', 87)]
``````

It is possible to use the `cycle()` function from `itertools` to repeat values from the shorter list. This will allow `zip()` to iterate over all the elements from the longer list. In this example, `cycle()` is used to repeat values from `list_one` and the resulting merged list will now contain all values from `list_two`.

``````from itertools import cycle
list_one = ['Joe', 'Mark', 'Jane']
list_two = [ 100, 34, 87, 23, 65 ]
merged2 = zip(cycle(list_one), list_two)
print(list(merged2))
#[('Joe', 100), ('Mark', 34), ('Jane', 87), ('Joe', 23), ('Mark', 65)]
``````
34 Likes

Hello,
Why the sublists created by “zip” command are coming out in curly brackets ( ) and not in square brackets to identify that it is essentially a list?

8 Likes

look at the below example:

``````myList = ['a', 'b', 'c']
myTuple = ('a', 'b', 'c')
``````

we have a list (`myList`) and a tuple (`myTuple`), a tuple is very similar to a list, except tuples are immutable.

Given values returned by zip don’t have to be mutated, a tuple is more efficient.

https://www.tutorialspoint.com/python/python_tuples.htm

26 Likes

Thank for your fast and thorough response, it has been very helpful!

3 Likes

Can we merge more than 2 list with this zip tool?

1 Like

Some questions we can answer by trying…

``````>>> a, b, c = [1, 2, 3], [4, 5, 6], [7, 8, 9]
>>> list(zip(a, b, c))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>>
``````
11 Likes

Thanks for the explanation.
Had to google what immutable meant, for those who are also on the same boat I was it means it cannot change.

8 Likes

Can you explain how to use cycle().

`cycle()` can be used to pair up characters from a short sequence with characters in a long sequence.

``````a = list('abcdefghijklmnopqrstuvwxyz')
b = [1, 2, 3, 4]
``````

Now we can zip the two together. Normally `zip()` will stop when it reaches the end of the short sequence so when we do this,

``````x = list(zip(a, b))
``````

we get,

``````[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
``````

but when we use `cycle()` on the short sequence we get,

``````z = list(zip(a, cycle(b)))

[('a', 1), ('b', 2), ('c', 3), ('d', 4),
('e', 1), ('f', 2), ('g', 3), ('h', 4),
('i', 1), ('j', 2), ('k', 3), ('l', 4),
('m', 1), ('n', 2), ('o', 3), ('p', 4),
('q', 1), ('r', 2), ('s', 3), ('t', 4),
('u', 1), ('v', 2), ('w', 3), ('x', 4),
('y', 1), ('z', 2)]``````
7 Likes

Nice explanation. Thank you !

2 Likes

Why I’m getting error as cycle() is not defined
a = [1, 2, 3, 4]

b = [“x”, “y”, “z”]

zipping_cycle = zip(a, cycle(b))

new_list2 = list(zipping_cycle)

print(new_list2)

Did you import cycle from itertools?

Wow thanks for the cycle function, it was helpful!