-1

input: list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] wanted output: list1 = [1, 3, 5, 7, 9], list2 = [2, 4, 6, 8, 10]

How can I do this?

  • 1
    Does this answer your question? [Splitting a list into two seperate lists, by every other item in python](https://stackoverflow.com/questions/22162074/splitting-a-list-into-two-seperate-lists-by-every-other-item-in-python) – Mohnish Jul 11 '20 at 08:41
  • You may think about accepting an answer to reward those how helped you, or at least comment to explain what's missing ;) – azro Jul 19 '20 at 12:51
  • I am really new to stackoverflow. I saw: 'the question already has answers here:' so I was thinking this ended my question. – Philip Bergwerf Jul 23 '20 at 09:02

1 Answers1

1

You can just use slice notation with third param that is increment

values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
v1, v2 = values[::2], values[1::2]
print(v1) # [1, 3, 5, 7, 9]
print(v2) # [2, 4, 6, 8, 10]
azro
  • 53,056
  • 7
  • 34
  • 70