How to split Python tuples into sub-tuples?


In this article, we will show you how to split python tuples into sub-tuples. Below are the various methods to accomplish this task −

  • Using slicing

  • Using enumerate() & mod operator

Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable.

Using slicing

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input tuple.

  • Print the input tuple.

  • To Split the input tuple into n(3) elements, Using the range() function(returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number), loop from 0 to the length of the tuple using len() function(The number of items in an object is returned by the len() method), with n as the step value.

  • Store all data from the current iterator to the current iterator index +n values for each iteration (sub tuples)

  • Print the resultant tuple after splitting it into group of 4 tuples.

Example

The following program splits the given python tuples into sub-tuples using slicing −

# input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # Giving the Number of sub-tuples required n = 3 # splitting the input tuple with n(3) elements # Looping from 0 indexes to length of tuple with n as step value using the range() function # For each iteration storing all values from the current iterator to the current iterator index +n values(sub tuples) resultTuple = tuple(inputTuple[i:i + n] for i in range(0, len(inputTuple), n)) # printing the result tuple print ("Splitting the input tuple into the group of 4-tuples with 3 elements in each:\n", resultTuple)

Output

On executing, the above program will generate the following output −

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4-tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

Using enumerate() & mod operator

The enumerate() method adds a counter to an iterable and returns the enumerate object.

Syntax

enumerate(iterable, start=0)

Parameters

  • iterable − It can be any sequence/object/iterable supporting iteration

  • start − enumerate() begins counting from this value. If the start is not specified, the value 0 is used.

Modulo Operator(%)

In Python, the % symbol is known as the Modulo Operator. It gives the result of dividing the left-hand operand by the right-hand operand. It's used to solve division problems by obtaining the remainder.

Example

The following program splits the given python tuples into sub-tuples using enumerate() and mod operator −

# input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # splitting the input tuple with n(3) elements # Looping in the given tuple using the enumerate() function # Splitting into tuples of size 3 so we used n%3==0 condition resultTuple = tuple(inputTuple[e:e + 3] for e, k in enumerate(inputTuple) if e % 3 == 0) # printing the result tuple print ("Splitting the input tuple into the group of 4-tuples with 3 elements in each:\n", resultTuple)

Output

On executing, the above program will generate the following output −

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4-tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

Using itertools receipes

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a function splitTupleandGroup() using the def keyword to split the input tuple into groups of k tuples(here 4 tuples with 3 elements in each) by passing the k, input tuple as arguments to it

  • Pass the given input tuple to the iter() function(The iter() function in Python returns an iterator object, which is used to transform an iterable to an iterator) and convert it to a list using the [] operator and multiply it with the number of sub-tuples needed(k).

  • Combine all the above sub-lists using the zip() function(The zip() function can be used to combine two lists/iterators) by passing the sub lists by unpacking them using the * operator.

  • Create a variable to store the input tuple.

  • Print the input tuple.

  • Call the splitTupleandGroup() function to group to create sub-tuples.

  • Print the resultant tuple after splitting it into group of 4 tuples.

Example

The following program splits the given python tuples into sub-tuples using iter() and zip() functions −

# creating a function to split the input tuple into groups of # k tuples(here 4-tuples with 3 elements) def splitTupleandGroup(k, inputTuple): # Multiply the given tuple with the number of sub-tuples needed args = [iter(inputTuple)] * k # Return the zip object of the above args return zip(*args) # input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # calling the splitTupleandGroup function to group the k elements of the tuple # and converting into a tuple resultTuple = tuple(splitTupleandGroup(3, inputTuple)) # printing the result tuple print ("Splitting the input tuple into a group of 4-tuples with 3 elements in each:\n", resultTuple)

Output

On executing, the above program will generate the following output −

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4 tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

Conclusion

In this post, we learnt three alternative ways to divide a Python tuple into sub-tuples. We also learned how to convert a tuple to an iterator. How can I convert an iterator to a list, Unpacking the lists and returning them as a tuple

Updated on: 09-Nov-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements