Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List containing only every second second pair of elements

Tags:

python

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

oldlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

then I want that the new list looks like this

newlist = [3, 4, 7, 8]

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.

like image 847
Karel Měřínský Avatar asked Jun 24 '21 19:06

Karel Měřínský


People also ask

How do you get every second element in a list?

To return every second element we have to change the start value to 1. Now, create a function that uses a for loop and the range function to generate list indexes. The first argument (start) is equal to 1 and the second argument (end) is equal to the length of the list.

How to return every second element of a list in Python?

To return every second element we have to change the start value to 1. >>> list(range(1,5,2)) [1, 3] Now, create a function that uses a for loop and the range function to generate list indexes. The first argument (start) is equal to 1 and the second argument (end) is equal to the length of the list.

How to print the second element of a list?

That’s handy because you don’t have to handle the index yourself avoiding potential errors when working with indexes. Use a list comprehension and the enumerate function to print every second element of a list.

How to make a list that only contains every second value?

def every_second_element(values):   second_values = []    for index in range(1, len(values), 2):        second_values.append(values[index])    return second_values We use the list append methodto create a new list that only contains every second value, we then return the new list at the end of the function.

How to split a linked list into two lists in Python?

Given a linked list, split it into two lists containing alternating elements from the original list. The elements in the new lists may be in any order. For example, if the original list is {a, b, a, b, a}, then one sublist should be {a, a, a} and the other should be {b, b}. 1. Using moveNode () function


4 Answers

If you enumerate the list, you'd be taking those entries whose indices give either 2 or 3 as a remainder when divided by 4:

>>> [val for j, val in enumerate(old_list) if j % 4 in (2, 3)]

[3, 4, 7, 8]
like image 186
Mustafa Aydın Avatar answered Oct 21 '22 06:10

Mustafa Aydın


a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [a[i]  for i in range(len(a)) if i%4 in (2,3)]

# Output: b = [3, 4, 7, 8]

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.

like image 35
lifezbeautiful Avatar answered Oct 21 '22 07:10

lifezbeautiful


first_part = oldList[2::4] # every 4th item, starting from the 3rd item
second_part = oldList[3::4] # every 4th item starting from the 4th item

pairs = zip(first_part, second_part)
final_result = chain.from_iterable(pairs)
like image 40
Lagerbaer Avatar answered Oct 21 '22 07:10

Lagerbaer


Break this problem in to parts.

first = oldlist[2::4]
second = oldlist[3::4]
pairs = [(x, y) for x, y in zip(first, second)]

Now unwrap the pairs:

newlist = [x for p in pairs for x in p]

Combining:

newlist = [z for p in [(x, y) for x, y in zip(oldlist[2::4], oldlist[3::4])] for z in p]
like image 3
Ben Y Avatar answered Oct 21 '22 06:10

Ben Y