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
# 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)
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
# 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)
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
# Python3 code to demonstrate working of
# Convert tuple to List with succeeding element
# initializing tuple
test_tup = (5, 6, 7, 4, 9)
# printing original tuple
print("The original tuple is : ", test_tup)
# initializing K
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)
# printing result
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
# initializing tuple
test_tup = (5, 6, 7, 4, 9)
# printing original tuple
print("The original tuple is : ", test_tup)
# initializing K
K = "Gfg"
# using map
res = list(map(lambda x: [x, K], test_tup))
res = [j for i in res for j in i]
# printing result
print("Converted Tuple with K : ", res)
#This code is contributed by Vinay Pinjala.
OutputThe 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
# Python3 code to demonstrate working of
# Convert tuple to List with succeeding element
# Using recursion
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)
# initializing tuple
test_tup = (5, 6, 7, 4, 9)
# printing original tuple
print("The original tuple is : ", test_tup)
# initializing K
K = "Gfg"
res = tuple_to_list_with_k(test_tup,K)
# printing result
print("Converted Tuple with K : ", res)
OutputThe 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.
Similar Reads
Python program to convert a list of strings with a delimiter to a list of tuple
Given a List containing strings with a particular delimiter. The task is to remove the delimiter and convert the string to the list of tuple. Examples: Input : test_list = ["1-2", "3-4-8-9"], K = "-" Output : [(1, 2), (3, 4, 8, 9)] Explanation : After splitting, 1-2 => (1, 2). Input : test_list =
7 min read
Python program to Convert a elements in a list of Tuples to Float
Given a Tuple list, convert all possible convertible elements to float. Input : test_list = [("3", "Gfg"), ("1", "26.45")] Output : [(3.0, 'Gfg'), (1.0, 26.45)] Explanation : Numerical strings converted to floats. Input : test_list = [("3", "Gfg")] Output : [(3.0, 'Gfg')] Explanation : Numerical str
5 min read
Python Program to Convert a list of multiple integers into a single integer
There are times when we need to combine multiple integers from a list into a single integer in Python. This operation is useful where concatenating numeric values is required. In this article, we will explore efficient ways to achieve this in Python.Using join()join() method is the most efficient an
2 min read
Python program to extract characters in given range from a string list
Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
4 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set metho
7 min read
Python Program to Convert a List to String
In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it.Using the
3 min read
Convert list of strings to list of tuples in Python
Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Convert List Of Tuples To Json String in Python
We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python. Convert List Of Tuples To Json String in PythonBelow, are the methods of Convert List Of Tuples To Json St
3 min read
How To Convert Comma-Delimited String to a List In Python?
In Python, converting a comma-separated string to a list can be done by using various methods. In this article, we will check various methods to convert a comma-delimited string to a list in Python.Using str.split()The most straightforward and efficient way to convert a comma-delimited string to a l
1 min read
Concatenate all Elements of a List into a String - Python
We are given a list of words and our task is to concatenate all the elements into a single string with spaces in between. For example, given the list: li = ['hello', 'geek', 'have', 'a', 'geeky', 'day'] after concatenation, the result will be: "hello geek have a geeky day".Using str.join()str.join()
2 min read