Skip certain indices in a Python list

0

I have the following situation:

  • I have a list of 64 items and I want the first two values to appear and all the others after 2.

An example would be:

lista = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]

I want the values to be displayed: [1,2,5,6,9,10,13,14,17,18,21,22]

I thank you sincerely for the help of those who can.

    
asked by anonymous 08.03.2017 / 00:41

1 answer

3
[lista[i:i+2] for i in range(0, len(lista), 4)]

First, I used the list compression engine to generate the new list. The values in this list will always be the values of the original list in the indexes i to i+2 for each value of i . With for set that the i values will be 0 to the length of the original list, with a step of 4. That is, for i=0 , I add the values on indexes 0 and 1 to the list. next value of i will be 4, adding values of indices 4 and 5, so indices 2 and 3 have been ignored. The same happens for values greater than i .

See working at Repl.it .

    
08.03.2017 / 01:01