Python program to convert 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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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
# Python3 code to demonstrate working of# Convert tuple to List with succeeding element# Using list comprehension# initializing tupletest_tup = (5, 6, 7, 4, 9)# printing original tupleprint("The original tuple is : ", test_tup)# initializing KK = "Gfg"# list comprehension for nested loop for flattenres = [ele for sub in test_tup for ele in (sub, K)]# printing resultprint("Converted Tuple with K : ", res) |
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
# Python3 code to demonstrate working of# Convert tuple to List with succeeding element# Using chain.from_iterable() + list() + generator expressionfrom itertools import chain# initializing tupletest_tup = (5, 6, 7, 4, 9)# printing original tupleprint("The original tuple is : ", test_tup)# initializing KK = "Gfg"# list comprehension for nested loop for flattenres = list(chain.from_iterable((ele, K) for ele in test_tup))# printing resultprint("Converted Tuple with K : ", res) |
Output:
The original tuple is : (5, 6, 7, 4, 9)
Converted Tuple with K : [5, ‘Gfg’, 6, ‘Gfg’, 7, ‘Gfg’, 4, ‘Gfg’, 9, ‘Gfg’]


