본문 바로가기
● 알고리즘, 자료구조/2019 알고리즘

[py/Error] cannot unpack non-iterable int object

by 0ver-grow 2019. 8. 1.
반응형

https://www.reddit.com/r/learnpython/comments/aswvgu/why_am_i_getting_an_typeerror_cannot_unpack/

 

Why am I getting an 'TypeError: cannot unpack non-iterable int object' when I try to iterate over a tuple here?

I meant unpack, not iterate in the title** a = [(1,2), (3,4)] for item in a: for i,j in item: # <------ TypeError: cannot...

www.reddit.com

When you use the for i,j in item syntax, Python is expecting that item is an iterable of iterables, which can be unpacked into the variables i and j. In this case, when you iterate over item, the individual members of item are ints, which cannot be unpacked.

If you want to unpack the items in the tuple, you should do it like this:

for i, j in a: # This unpacks the tuple's contents into i and j as you're iterating over a print(i, j)

반응형