Scroll through a list inside another List

Asked

Viewed 256 times

2

Hello I’m a beginner in Python and I wonder if there is how to scroll through a list within another list, I tried to make an example, but is giving this error: "Typeerror: 'int' Object is not subscriptable", if anyone can help me, from now on aggravate

result = ['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
for i in range (len(result)): # procurar em todas as listas internas
for j in range (i): 
    print(result[i][j])
    

1 answer

2

This error occurs because you are trying to scroll through a number.

Using his example, he tries to iterate the elements 'spam' and '1'. As they are not a list, the error occurs.

One way to solve this problem is by placing a check before going through the internal items.

result = ['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
for i in result:
    print(i)
    if type (i) == list:
       for j in i:
           print(j)

I changed the way I iterate a little bit, but it is possible to use your way of going through the lists.

  • Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.