Python – Concatenate consecutive elements in Tuple

Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about 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 generator expression to provide concatenation logic and simultaneous element pairing is done by zip(). The result is converted to tuple form using tuple().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Consecutive element concatenation in Tuple
# using zip() + generator expression + tuple
  
# 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 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))

chevron_right


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 above functions can also help to perform this task. In this, we perform concatenation and binding logic using lambda function. The map() is used to iterate to each element and at end result is converted by tuple().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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')


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.