TypeError: 'zip' object is not callable in Python 3.x

Question:

I’m trying to zip 2 simple lists in Python 3.5, and can’t figure out how to access the contents. I’ve read now that zip() is a generator, and I have to call list(zip()) if I want to store the contents. However, I’m experiencing an error when doing so.

For example:

row = [1, 2, 3]
col = ['A', 'B', 'C']
z = zip(row, col)

When I call ‘z’, I see the following:

>>> z
<zip object at 0x0283C8F0>

But when I call list(z), I get the following error:

>>> list(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'zip' object is not callable

I also get the same error when attempting to assign the list to a variable, such as:

l = list(zip(row, col))

Apologies if this has already been answered, but I cannot for the life of me find a solution via search. Any ideas would be greatly appreciated!!

Asked By: Aaron Warnecke

||

Answers:

that happens when you redefine list as zip() (which is probably what you did but didn’t show us):

>>> list = zip()

now list is a zip object (not the zip class)

>>> list(z)

now you’re attempting to call a zip object

Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: 'zip' object is not callable
>>> 

just

del list

(or start from a fresh interpreter)

and everything’s back to normal

If you’re trying to use this in a .map re-assignment, you’d have to wrap it in dict, e.g.

df.Account = df.Account.map(dict(zip(list1, list2)))
Answered By: hitmikey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.