Getting errors when computing the length of list of lists

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:

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.

>Solution :

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]

Leave a Reply