I have a list that will always contain an even number of elements and I want to iterate over this list to create a new list containing lists of each 2 consecutive numbers in the list.
For example:
first_list = [1,2,3,4,5,6,7,8]
second_list = [[1,2], [3,4], [5,6], [7,8]]
When I iterate over the list I cannot figure out how to select consecutive pairs. I've tried a million variations, and this is the closest that I've come.
first_list = [1,2,3,4,5,6,7,8]
second_list = []
pairs = 1
for item in range(len(first_list) - pairs):
second_list.append([firs_list[item],first_list[item + pairs]])
print second list
[[1, 5], [5, 7], [7, 6], [6, 2], [2, 3], [3, 4], [4, 8]]
Is there some way that I have have the for loop iterate over every other item?