How to split Python tuples into sub-tuples?

PythonServer Side ProgrammingProgramming

Here is a tuple with 12 integers. In order to split it in four sub-tuple of three elements each, slice a tuple of three successive elements from it and append the segment in a lis. The result will be a list of 4 tuples each with 3 numbers.

>>> tup=(7,6,8,19,2,4,13,1,10,25,11,34)
>>> lst=[]
>>> for i in range(0,10,3):
lst.append((tup[i:i+3]))
>>> lst
[(7, 6, 8), (19, 2, 4), (13, 1, 10), (25, 11, 34)]
raja
Published on 03-Mar-2018 00:19:06
Advertisements