-2

I want to use list comp' instead of a loop. Let's sat I have a list of lists and I want only the even index elements. Eg: [[a,a], [b,b], [c,c],[g,g]] to..... [[a,a], [c,c]]

This doesn't work


a = [i for i in the_list if i % 2 == 0)]

tibetish
  • 109
  • 8

1 Answers1

1

Here is a possible solution that keeps only lists with even indexes:

result = [lst for i, lst in enumerate(the_list) if i % 2 == 0]
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50