1

new_list =[0,0,0,0]

for x_list in random_list: # list of list

for x in x_list:

if x == "I" or "i":

list_index = x_list.index(x)

new_list[list_index] += 1

Lets say the random_list was [['x','x','I','I'],['x','x','I','x']]

it should output [0,0,2,1], but it doesn't

1 Answers1

0

You may be looking for the built-in enumerate, which allows you to iterate over the objects and indices at the same time. your inner loop could then be

for list_index, x in enumerate(x_list):
    if x == "I" or "i":
        new_list[list_index] += 1

and it should do what you want

PS

One more gotcha: if x == "I" or "i": should actually be if x == "I" or x == "i":

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72