Skip to content
Advertisement

List containing only every second second pair of elements

I am new to python and so I am experimenting a little bit, but I have a little problem now.

I have a list of n numbers and I want to make a new list that contains only every second pair of the numbers.

So basically if I have list like this

JavaScript

then I want that the new list looks like this

JavaScript

I already tried the slice() function, but I didn’t find any way to make it slice my list into pairs. Then I thought that I could use two slice() functions that goes by four and are moved by one, but if I merge these two new lists they won’t be in the right order.

Advertisement

Answer

JavaScript

Here, we use the idea that the 3rd,4th,7th,8th..and so on. indices leave either 2 or 3 as the remainder when divided by 4.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement