Open In App

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

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.


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:

  1. Define a function called tuple_to_list_with_k that takes a tuple and a value k as arguments.
  2. Check if the tuple is empty. If it is, return an empty list.
  3. 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.
  4. 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)


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.



Similar Reads

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 method is to convert other type values to set this meth
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list. Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12
11 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Python - Extract Kth element of every Nth tuple in List
Given list of tuples, extract Kth column element of every Nth tuple. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3 Output : [3, 8, 10] Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6,
8 min read
Python | Convert a List into a Tuple
Given a list, write a Python program to convert the given list into a Python tuple. Examples: Input : [1, 2, 3, 4]Output : (1, 2, 3, 4)Input : ['a', 'b', 'c']Output : ('a', 'b', 'c')Convert a list into a tuple using tuple()Using tuple(list_name). Typecasting to tuple can be done by simply using tuple(list_name). C/C++ Code # Python3 program to conv
3 min read
Python | Convert a list into tuple of lists
We are given a list, the task is to convert the list into tuple of lists. Input: ['Geeks', 'For', 'geeks'] Output: (['Geeks'], ['For'], ['geeks'])Input: ['first', 'second', 'third'] Output: (['first'], ['second'], ['third']) Method #1: Using Comprehension C/C++ Code # Python code to convert a list into tuple of lists # Initialisation of list Input
4 min read