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 Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read