Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!