Open In App

Create a tuple from string and list – Python

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)



Last Updated : 23 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads