How can I change the grouping of items in a list?
3 answers
Using the concepts of zip and list, assuming the datatype is converted from a list of int to a list of strings:
In [1]: x = [0,1,2,3,4,5,6,7,8,9]
In [2]: pairs = zip(x[::2], x[1::2])
In [3]: pairs
Out[3]: [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
In [4]: [str(fst) + str(snd) for fst, snd in pairs]
Out[4]: ['01', '23', '45', '67', '89']
+2
source to share