Create a tuple from string and list – Python
Last Updated :
23 Jun, 2023
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
test_list = [ "gfg" , "is" ]
test_str = "best"
print ( "The original list : " + str (test_list))
print ( "The original string : " + test_str)
res = tuple (test_list + [test_str])
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
test_list = [ "is" , "best" ]
test_str = "gfg"
print ( "The original list : " + str (test_list))
print ( "The original string : " + test_str)
res = (test_str, ) + tuple (test_list)
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
original_list = [ 'gfg' , 'is' ]
original_string = 'best'
new_list = original_list.copy()
new_list.append(original_string)
new_tuple = tuple (new_list)
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
- Start by defining a function that takes two arguments, a string, and a list.
- Extend the given list with the given string using the extend() function.
- Convert the extended list to a tuple using the tuple() function.
- Return the tuple.
Python3
def create_tuple(string, lst):
lst.extend([string])
return tuple (lst)
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)
Please Login to comment...