Open In App

Create a tuple from string and list – Python

Last Updated : 23 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, we can have a problem in which we need to construct a new container with elements from different containers. This kind of problem can occur in domains in which we use different types of data. Let’s discuss ways to convert string and list data to tuple. 

Create a tuple from string and list Using List conversion to tuple + tuple() 

In this method, we convert the string to list and then append to target list and then convert this result list to tuple using tuple()

Python3




# Python3 code to demonstrate working of
# Construct tuple from string and list
# using list conversion to tuple + tuple()
 
# initialize list and string
test_list = ["gfg", "is"]
test_str = "best"
 
# printing original list and tuple
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
# Construct tuple from string and list
# using list conversion to tuple + tuple()
res = tuple(test_list + [test_str])
 
# printing result
print("The aggregated tuple is : " + str(res))


Output : 

The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ('gfg', 'is', 'best')

Create a tuple from string and list Using Tuple conversion to tuple + tuple() 

This is another way in which this task can be performed. In this, we convert the string and list both to tuple and add them to result tuple. This method is more efficient than the above method. 

Python3




# Python3 code to demonstrate working of
# Construct tuple from string and list
# using tuple conversion to tuple + tuple()
 
# initialize list and string
test_list = ["is", "best"]
test_str = "gfg"
 
# printing original list and tuple
print("The original list : " + str(test_list))
print("The original string : " + test_str)
 
# Construct tuple from string and list
# using tuple conversion to tuple + tuple()
res = (test_str, ) + tuple(test_list)
 
# printing result
print("The aggregated tuple is : " + str(res))


Output : 

The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ('gfg', 'is', 'best')

Create a tuple from string and list Using list+ tuple+ copy()+append()

Get the copy of original list and then append the original string to it later convert it to tuple

step-by-step algorithm

1. we first make a copy of the original list

2. we use the append() method to add the elements of the original string to the new list. 

3. We then create a new tuple from the new list using the tuple() constructor. 

4.The resulting tuple contains all the elements of the original list followed by the original string.

Python3




# Define the original list and string
original_list = ['gfg', 'is']
original_string = 'best'
 
# Create a new list by extending the original list with the string
new_list = original_list.copy()
new_list.append(original_string)
 
# Create a new tuple from the new list using the tuple constructor
new_tuple = tuple(new_list)
 
# Print the output tuple
print("The aggregated tuple is :", new_tuple)


Output

The aggregated tuple is : ('gfg', 'is', 'best')

Time Complexity: O(n), where n is the length of the list.
Space Complexity: O(n), where n is the length of the list (due to the creation of the new list and tuple).

Create a tuple from string and list Using the + operator

In this approach, we use the + operator to concatenate the list and the string, and then convert the result to a tuple using the tuple() constructor.

1.Concatenate the list and the string using the + operator.
2.Convert the concatenated result to a tuple using the tuple() constructor.
3.Return the tuple.

Python3




lst = ['gfg', 'is']
string = 'best'
 
result = tuple(lst + [string])
 
print(result)


Output

('gfg', 'is', 'best')

Time complexity: O(n), where n is the length of the list.

Auxiliary Space: O(n), where n is the length of the list. The new list created by concatenation takes O(n) space, and the resulting tuple also takes O(n) space.

Create a tuple from string and list Using extend method

The approach used here is to create a new list by extending the original list with the given string and then convert the new list to a tuple using the built-in tuple() function

  1. Start by defining a function that takes two arguments, a string, and a list.
  2. Extend the given list with the given string using the extend() function.
  3. Convert the extended list to a tuple using the tuple() function.
  4. Return the tuple.

Python3




# Function to create a tuple
def create_tuple(string, lst):
    lst.extend([string])
    return tuple(lst)
 
# Driver Code
original_list = ['gfg', 'is']
original_string = 'best'
 
result = create_tuple(original_string, original_list)
 
print("The original list : ", original_list)
print("The original string : ", original_string)
print("The aggregated tuple is : ", result)


Output

The original list :  ['gfg', 'is', 'best']
The original string :  best
The aggregated tuple is :  ('gfg', 'is', 'best')

The time complexity of the algorithm is O(n)

The space complexity of the algorithm is O(n)



Previous Article
Next Article

Similar Reads

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 - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
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 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 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 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 create a list of tuples from given list having number and its cube in each tuple
Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number. Example: Input: list = [1, 2, 3] Output: [(1, 1), (2, 8), (3, 27)] Input: list = [9, 5, 6] Output: [(9, 729), (5, 125), (6, 216)] Method #1 : Using pow() function.We can use list compreh
5 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read
three90RightbarBannerImg