Home > Enterprise >  Getting errors when computing the length of list of lists
Getting errors when computing the length of list of lists

Time:06-16

I have a list of lists. I am trying to find the indices of lists whose length is 4. Here is the code:

for i, j in enumerate(list_of_lists):
  if [x for x in list_of_lists if len(x) == 4] in j:
    print(i)

Alongside the correct indices, I also get the index of a list whose length is 1. Is there something wrong in my code? I have no idea why this happens.

CodePudding user response:

for i, j in enumerate(list_of_lists):
  if len(j) == 4:
    print(i)

CodePudding user response:

If you prefer to collect the indices instead of printing them, it is even simpler:

indices = [i for i, j in enumerate(list_of_lists) if len(j) == 4]
  • Related