The Zip() Function in Python 3

The zip() function in Python 3

Unlike in Python 2, the zip function in Python 3 returns an iterator. Iterators can only be exhausted (by something like making a list out of them) once. The purpose of this is to save memory by only generating the elements of the iterator as you need them, rather than putting it all into memory at once. If you want to reuse your zipped object, just create a list out of it as you do in your second example, and then duplicate the list by something like

 test2 = list(zip(lis1,lis2))
zipped_list = test2[:]
zipped_list_2 = list(test2)

Difference between zip() functions in Python 2 and Python 3

Difference between Python 2 and Python 3 is Python 3 returns an iterators. Idea of this saving memory.

code equivalent in Python 3 for zip related statements

Try this:

m = list(zip(*m[1:len(m)]))[::-1]

In Python 3, zip() return an iterator. Wrapping it in list() will run the iterator to create list, giving the same behavior as Python 2.

Python zip function - weird result

You are using Python 3. In Python 3, zip() returns an iterator instead of a list.

Iterators evaluation is lazy: only one item is returned at a time, and there is no __len__. Iterators are made to work best for loop: indeed, most of the time zip() will be used in a loop, where you only need one item at a time, saving memory and CPU cycles.

Just try to do list(zip(a,b)) to force the evaluation of the iterator into a list.

zip function in python for lists

The issue is that zip is not used to achieve merging your lists

Here is the correct implementation

list1 = []
list2 = [0]
result=list1+list2
print(result)

# result output: [0]

However, zip function is used as an iterator between lists. below is an example to help you with the concept

a = ("1", "2", "3")
b = ("A", "B", "C")

x = zip(a, b)

for i in x: # will loop through the items in x
print(i)

# result output:
# ('1', 'A')
# ('2', 'B')
# ('3', 'C')

Zip lists in Python

When you zip() together three lists containing 20 elements each, the result has twenty elements. Each element is a three-tuple.

See for yourself:

In [1]: a = b = c = range(20)

In [2]: zip(a, b, c)
Out[2]:
[(0, 0, 0),
(1, 1, 1),
...
(17, 17, 17),
(18, 18, 18),
(19, 19, 19)]

To find out how many elements each tuple contains, you could examine the length of the first element:

In [3]: result = zip(a, b, c)

In [4]: len(result[0])
Out[4]: 3

Of course, this won't work if the lists were empty to start with.

what does zip(*res) mean in python in the following code?

In python, * is the 'splat' operator. It is used for unpacking a list into arguments. For example: foo(*[1, 2, 3]) is the same as foo(1, 2, 3).

The zip() function takes n iterables, and returns y tuples, where y is the least of the length of all of the iterables provided. The yth tuple will contain the yth element of all of the iterables provided.

For example:

zip(['a', 'b', 'c'], [1, 2, 3])

Will yield

('a', 1) ('b', 2) ('c', 3)

For a nested list like res in the example you provided, calling zip(*res) will do something like this:

res = [['a', 'b', 'c'], [1, 2, 3]]
zip(*res)
# this is the same as calling zip(['a', 'b', 'c'], [1, 2, 3])
('a', 1)
('b', 2)
('c', 3)

Python zip function with iterator vs iterable

The difference is that for [iter(string)] * 3, zip creates aliases of a single iterator. For [string] * 3, zip creates unique iterators per argument. The shorter output without duplicates is zip exhausting the single aliased iterator.

See what is meaning of [iter(list)]*2 in python? for more details on how [iter(...)] * 2 works and causes potentially unexpected results.

See the canonical answer List of lists changes reflected across sublists unexpectedly if the [...] * 3 aliasing behavior is surprising.



Related Topics



Leave a reply



Submit