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.
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.
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.
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.
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.
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
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]
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.
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)
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With