list of ints into a list of tuples python
list, python
Solution
You can use zip combined with slicing to create a new list of tuples.
my_new_list = zip(my_list[0::2], my_list[1::2])
This would generate a new list with the following output
[(1, 109), (2, 109), (2, 130), (2, 131), (2, 132), (3, 28), (3, 127)]
The process behind this is quite simple. We first split the existing list into two new lists using slicing.
print my_list[0::2] # [1, 2, 2, 2, 2, 3, 3]
print my_list[1::2] # [109, 109, 130, 131, 132, 28, 127]
Then use zip to combine these two lists into one list of tuples.
print zip(my_list[0::2], my_list[1::2])
Problem
`[1, 109, 2, 109, 2, 130, 2, 131, 2, 132, 3, 28, 3, 127]` I have this array and I want to turn it into a list of tuples, each tuple has 2 values in. so this list becomes `[(1, 109), (2, 109), (2, 130), (2, 131), (2, 132), (3, 28), (3, 127)]`