list looping error

G

Gigs_

i have created list x:
>>> x = [(12, 22, 11), (13, 22, 33)]

and want to print each number in tuple for j in i:
print j[0]

but I get this error. What does it means?
Traceback (most recent call last):
File "<pyshell#60>", line 3, in <module>
print j[0]
TypeError: 'int' object is unsubscriptable


thanks
 
T

tkpmep

What you really want to write is
for i in x:
for j in i:
print j

The outer loop iterates over the tuples in the list, while the inner
loop iterates over the elements of each tuple. So j (in your example)
is always an integer, and is therefore unsubscriptable, which is
exactly what the error message says.

Thomas Philips
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top