Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
List indices must be not list

I need to take action when pole[i][j] == 1, but it throws an error:

TypeError: list indices must be integers or slices, not list

pole = [[0,1,0],
        [1,0,1],
        [0,1,1]]
for i in pole:
    for j in pole:
        if pole[i][j] == 1:
            my_code()
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You use for i in pole which gets the actual values of pole not the indices. Here is the corrected code:

pole = [[0,1,0],
        [1,0,1],
        [0,1,1]]
for i in range(len(pole)):
    for j in range(len(pole[i])):
        if pole[i][j] == 1:
            my_code()

Alternatively, you could use the values not the indices so:

pole = [[0,1,0],
        [1,0,1],
        [0,1,1]]
for i in pole:
    for j in i:
        if j == 1:
            my_code()
over 4 years ago · Santiago Trujillo Relatório

0

a for-in loop iterates over the elements of a list, not its indexes. Also, note you have the inner loop performing the same iteration as the outer loop, not iterating over the the sublist:

for x in pole:
    for y in x:
        if y == 1:
            my_code()
over 4 years ago · Santiago Trujillo Relatório

0

Well,technically speaking, given that my_code() does not use i or j, you could do something like this:

for _ in range(sum(map(sum, pole))):
    my_code()

The elements in pole do not necessarily need to be 0s and 1s - you could trigger other conditions, for example:

sum(map(lambda x: sum(y > 0 for y in x), pole)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda