Python program to covert tuple into list by adding the given string after every element

Given a Tuple. The task is to convert it to List by adding the given string after every element.

Examples:

Input : test_tup = (5, 6, 7), K = "Gfg" 
Output : [5, 'Gfg', 6, 'Gfg', 7, 'Gfg'] 
Explanation : Added "Gfg" as succeeding element.

Input : test_tup = (5, 6), K = "Gfg" 
Output : [5, 'Gfg', 6, 'Gfg'] 
Explanation : Added "Gfg" as succeeding element.

Method #1: Using list comprehension

In this, we construct a tuple of each element of tuple with a succeeding element and then run a nested loop to flatten each constructed tuple using list comprehension.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert tuple to List with succeeding element
# Using list comprehension
  
# initializing tuple
test_tup = (5, 6, 7, 4, 9)
  
# printing original tuple
print("The original tuple is : ", test_tup)
  
# initializing K
K = "Gfg"
  
# list comprehension for nested loop for flatten
res = [ele for sub in test_tup for ele in (sub, K)]
  
# printing result
print("Converted Tuple with K : ", res)

chevron_right


Output:



The original tuple is :  (5, 6, 7, 4, 9)
Converted Tuple with K :  [5, ‘Gfg’, 6, ‘Gfg’, 7, ‘Gfg’, 4, ‘Gfg’, 9, ‘Gfg’]

Method #2 : Using chain.from_iterable() + list() + generator expression

This is similar to above method, difference is that nested loop is avoided by flattenning using chain.from_iterable().

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert tuple to List with succeeding element
# Using chain.from_iterable() + list() + generator expression
from itertools import chain
  
# initializing tuple
test_tup = (5, 6, 7, 4, 9)
  
# printing original tuple
print("The original tuple is : ", test_tup)
  
# initializing K
K = "Gfg"
  
# list comprehension for nested loop for flatten
res = list(chain.from_iterable((ele, K) for ele in test_tup))
  
# printing result
print("Converted Tuple with K : ", res)

chevron_right


Output:

The original tuple is :  (5, 6, 7, 4, 9)
Converted Tuple with K :  [5, ‘Gfg’, 6, ‘Gfg’, 7, ‘Gfg’, 4, ‘Gfg’, 9, ‘Gfg’]




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.