Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:
    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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading