Open In App

Python – Concatenate consecutive elements in Tuple

Last Updated : 28 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we need to find cumulative results. This can be of any type, product, or summation. Here we are gonna discuss adjacent element concatenation. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using zip() + generator expression + tuple()

The combination of above functionalities can be used to perform this task. In this, we use a generator expression to provide concatenation logic and simultaneous element pairing is done by zip(). The result is converted to tuple form using tuple(). 

Python3




# Python3 code to demonstrate working of
# Consecutive element concatenation in Tuple
# using zip() + generator expression + tuple
 
# Initializing tuple
test_tup = ("GFG ", "IS ", "BEST ", "FOR ", "ALL ", "GEEKS")
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
# Consecutive element concatenation in Tuple
# using zip() + generator expression + tuple
res = tuple(i + j for i, j in zip(test_tup, test_tup[1:]))
 
# Printing result
print("Resultant tuple after consecutive concatenation : " + str(res))


Output : 

The original tuple : ('GFG ', 'IS ', 'BEST ', 'FOR ', 'ALL ', 'GEEKS')
Resultant tuple after consecutive concatenation : ('GFG IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL GEEKS')

Method #2: Using tuple() + map() + lambda

The combination of the above functions can also help to perform this task. In this, we perform concatenation and binding logic using the lambda function. The map() is used to iterate to each element and at end result is converted by tuple(). 

Python3




# Python3 code to demonstrate working of
# Consecutive element concatenation in Tuple
# using tuple() + map() + lambda
 
# initialize tuple
test_tup = ("GFG ", "IS ", "BEST ", "FOR ", "ALL ", "GEEKS")
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Consecutive element concatenation in Tuple
# using tuple() + map() + lambda
res = tuple(map(lambda i, j: i + j, test_tup[: -1], test_tup[1:]))
 
# printing result
print("Resultant tuple after consecutive concatenation : " + str(res))


Output : 

The original tuple : ('GFG ', 'IS ', 'BEST ', 'FOR ', 'ALL ', 'GEEKS')
Resultant tuple after consecutive concatenation : ('GFG IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL GEEKS')

Method 3: Using Numpy module

Algorithm:

  1. Import the NumPy module.
  2. Initialize a tuple called test_tup.
  3. Use NumPy’s np.char.add() function to concatenate consecutive elements of test_tup.
  4. Convert the concatenated elements to a new tuple called res.
  5. Print the resulting tuple res.

Python3




import numpy as np
 
test_tup = ("GFG ", "IS ", "BEST ", "FOR ", "ALL ", "GEEKS")
 
res = tuple(np.char.add(test_tup[:-1], test_tup[1:]))
print("The original tuple : " + str(test_tup))
 
 
print("Resultant tuple after consecutive concatenation : " + str(res))


Output : 

The original tuple : ('GFG ', 'IS ', 'BEST ', 'FOR ', 'ALL ', 'GEEKS')
Resultant tuple after consecutive concatenation : ('GFG IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL GEEKS')

Time complexity: O(N), where N is the length of the input tuple, since each element in the tuple is accessed only once. 

Auxiliary space: O(N) since the resulting tuple will contain N concatenated elements. The np.char.add() function has a time complexity of O(N), where N is the length of the input arrays.

Method #4: Using a list comprehension and string concatenation

Python3




test_tup = ("GFG ", "IS ", "BEST ", "FOR ", "ALL ", "GEEKS")
 
# List compressing and concatinating string
res = tuple([test_tup[i] + test_tup[i+1] for i in range(len(test_tup)-1)])
 
print("The original tuple : " + str(test_tup))
print("Resultant tuple after consecutive concatenation : " + str(res))


Output

The original tuple : ('GFG ', 'IS ', 'BEST ', 'FOR ', 'ALL ', 'GEEKS')
Resultant tuple after consecutive concatenation : ('GFG IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL GEEKS')

Time complexity: O(n)
Auxiliary space: O(n), where n is the length of the input tuple.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads