I am trying to write a code that picks values in from a list to create to separate lists.
This is my code so far;
list = [6,2,9,10]
for x, y in zip(list, list):
print (x,y)
Output:
6,2
9,10
But what I want is:
[6,9]
[2,10]
I am trying to write a code that picks values in from a list to create to separate lists.
This is my code so far;
list = [6,2,9,10]
for x, y in zip(list, list):
print (x,y)
Output:
6,2
9,10
But what I want is:
[6,9]
[2,10]
You can zip slices of your original list to achieve your goal:
list = [6,2,9,10]
for x, y in zip(list[:2], list[2:]):
print (x,y)
6 9
2 10
This is not well-generalized - if you had a specific intent like "split my list into a first-half and second-half, and match the elements of those sub-lists" that would be more clear, but hopefully this helps you achieve what you want.
Assuming you want the elements at even index and odd index split into separate lists, you could do the following.
List1 =[1, 2,3,4,5]
even_list= List1[::2]
Odd_list = List1[1::2]