Python program to concatenate consecutive elements in tuple

In this program, we are given a tuple consisting of string elements. We need to create a Python program to concatenate consecutive elements in tuple.
Submitted by Shivang Yadav, on November 19, 2021

In Concatenating consecutive elements, we will concatenate the current element with the next element of the tuple for each element of the tuple.

Python programming language has a lot of features to extract fruitful data using cumulative results of data provided to it. These tasks are useful while working with data using python in complex applications like data science and Machine learning.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Concatenating consecutive elements in tuple

Here, we will be concatenating the current element with the very next element in the tuple and creating a new tuple with the concatenated elements. For this, we will loop over the tuple and concatenate. The resultant tuple will have one less element as the next element to the last element is not present.

Input:
('python', 'programming', 'language', 'learning')

Output:
('python programming', 'programming language', 'language learning')

In Python this task can be easily accomplished using build-it methods present in Python.

Method 1:

One method to solve the problem is by using generator expression to perform the concatenation operation using the zip() method for each element. Then convert the result to a tuple using the tuple() method.

# Python program to concatenate consecutive
# elements in tuple

# initialising and printing tuple 
myTuple = ("python ", "programming ", "language ", "tutorials ")
print("The elements of initial Tuple are "+str(myTuple))

# Concatenating consecutive elements in the tuple  
concatTuple = tuple(i + j for i, j in zip(myTuple, myTuple[1:]))
  
# printing Resultant tuple  
print("The elements of tuple created by concatenating consecutive elements are "+str(concatTuple))

Output:

The elements of initial Tuple are ('python ', 'programming ', 'language ', 'tutorials ')
The elements of tuple created by concatenating consecutive elements are ('python programming ', 'programming language ', 'language tutorials ')

Method 2:

Another method to solve the problem is by using a lambda function along with a map() to iterate tuple elements and concatenate them. After conversion, convert the resultant collection to a tuple using the tuple() method.

# Python program to concatenate 
# consecutive elements in tuple

# initialising and printing tuple 
myTuple = ("python ", "programming ", "language ", "tutorials ")
print("The elements of initial Tuple are "+str(myTuple))

# Concatenating consecutive elements in the tuple  
concatTuple = tuple(map(lambda i, j : i + j, myTuple[: -1], myTuple[1: ]))
  
# printing Resultant tuple  
print("The elements of tuple created by concatenating consecutive elements are "+str(concatTuple))

Output:

The elements of initial Tuple are ('python ', 'programming ', 'language ', 'tutorials ')
The elements of tuple created by concatenating consecutive elements are ('python programming ', 'programming language ', 'language tutorials ')

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.