Python program to convert tuple into list by adding the given string after every element
Last Updated :
10 Apr, 2023
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
test_tup = ( 5 , 6 , 7 , 4 , 9 )
print ( "The original tuple is : " , test_tup)
K = "Gfg"
res = [ele for sub in test_tup for ele in (sub, K)]
print ( "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’]
Time Complexity: O(n) where n is the number of elements in the tuple “test_tup”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #2 : Using chain.from_iterable() + list() + generator expression
This is similar to above method, difference is that nested loop is avoided by flattening using chain.from_iterable().
Python3
from itertools import chain
test_tup = ( 5 , 6 , 7 , 4 , 9 )
print ( "The original tuple is : " , test_tup)
K = "Gfg"
res = list (chain.from_iterable((ele, K) for ele in test_tup))
print ( "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’]
Time Complexity: The time complexity of this program is O(n), where n is the length of the input tuple test_tup.
Auxiliary Space: The auxiliary space used by this program is O(n), where n is the length of the input tuple test_tup.
Method #3 : Using list(),map(),join(),split() methods
Python3
test_tup = ( 5 , 6 , 7 , 4 , 9 )
print ( "The original tuple is : " , test_tup)
K = "Gfg"
x = list ( map ( str , test_tup))
b = "*" + K + "*"
a = b.join(x)
c = a.split( "*" )
c.append(K)
res = []
for i in c:
if (i ! = K):
res.append( int (i))
else :
res.append(i)
print ( "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 #4 : Using map() function
Python3
test_tup = ( 5 , 6 , 7 , 4 , 9 )
print ( "The original tuple is : " , test_tup)
K = "Gfg"
res = list ( map ( lambda x: [x, K], test_tup))
res = [j for i in res for j in i]
print ( "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']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using Recursive method.
Algorithm:
- Define a function called tuple_to_list_with_k that takes a tuple and a value k as arguments.
- Check if the tuple is empty. If it is, return an empty list.
- Otherwise, create a new list by concatenating the first element of the tuple with k, then recursively call the function on the rest of the tuple, and concatenate the result with the new list.
- Return the list.
Python3
def tuple_to_list_with_k(tup, k):
if not tup:
return []
else :
return [tup[ 0 ], k] + tuple_to_list_with_k(tup[ 1 :], k)
test_tup = ( 5 , 6 , 7 , 4 , 9 )
print ( "The original tuple is : " , test_tup)
K = "Gfg"
res = tuple_to_list_with_k(test_tup,K)
print ( "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']
Time complexity: O(n), where n is the length of the input tuple. This is because the function processes each element of the tuple exactly once.
Space complexity: O(n), where n is the length of the input tuple. This is because the function creates a new list to store the output, which can have up to 2n elements (each element of the input tuple is followed by k). Additionally, the function uses the call stack to handle recursive calls, which can have up to n levels of recursion.
Please Login to comment...