Python list (zipobject) returns empty (list) container

I got a weird problem in Python 3.4.3 and it is not mentioned anywhere.

Let's say:
a = [1,2,3,4]

andb = [5,6,7,8]

To merge them vertically: ab = zip(a,b)

in python 3, ab

itself will return:

zip object in (some hexometer)

Everything is fine here in python 3 for a merged list:
aabb = list(ab)

Now the problem, for the first time, aabb

will actually return a real list:
[(1, 5), (2, 6), (3, 7), (4, 8)]

A second time and onward, however, if you do the whole process again list(aabb)

it will just return an empty container []

like it would list()

.

It will only work after a shell / interpreter restart.

Is this normal or a bug?

EDIT : Ok guys, I didn't understand what it was related to zip

, it is SEEMED constant since ab

it was returning the same hex value every time, so I thought it was related to list(ab)

.

In any case, worked out by reassigning ab = zip(ab)

From what I understand in the answers and the original link is ab

set after reading.

+3


source to share


1 answer


This issue will not be generated list(aabb)

, but with list(ab)

in your code right now:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

ab = zip(a, b)
aabb = list(ab)

print(list(ab))

      

The problem is that there zip

is a generator that stores the values ​​once and then located like this:



a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

ab = zip(a, b)  # generator expression created by the zip function
aabb = list(ab) # element read from ab, disposed, placed in a list

print(list(ab)) # now ab has nothing because it was disposed

      

This, on the other hand, should work, because aabb

- it's just a list, not a disposed generator ab

:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

ab = zip(a, b)
aabb = list(ab)

print(list(aabb))

      

+2


source







All Articles