-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
  • 99
  • 7

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
  • 11,231
  • 3
  • 17
  • 42