Python – Adding Tuple to List and vice – versa
Last Updated :
26 Mar, 2024
Sometimes, while working with Python containers, we can have a problem in which we need to perform addition of one container with another. This kind of problem can have occurrence in many data domains across Computer Science and Programming. Let’s discuss certain ways in which this task can be performed.
Method 1 : Using += operator [list + tuple]
This operator can be used to join a list with a tuple. Internally its working is similar to that of list.extend(), which can have any iterable as its argument, tuple in this case.
Python3
# Python3 code to demonstrate working of
# Adding Tuple to List and vice - versa
# Using += operator (list + tuple)
# initializing list
test_list = [5, 6, 7]
# printing original list
print("The original list is : " + str(test_list))
# initializing tuple
test_tup = (9, 10)
# Adding Tuple to List and vice - versa
# Using += operator (list + tuple)
test_list += test_tup
# printing result
print("The container after addition : " + str(test_list))
OutputThe original list is : [5, 6, 7]
The container after addition : [5, 6, 7, 9, 10]
Time complexity: O(1) – The += operator takes constant time to add a tuple to a list.
Auxiliary space: O(1) – No additional space is used, as the original list and tuple are modified in place.
Method #2 : Using tuple(), data type conversion [tuple + list]
The following technique is used to add list to a tuple. The tuple has to converted to list and list has to be added, at last resultant is converted to tuple.
Python3
# Python3 code to demonstrate working of
# Adding Tuple to List and vice - versa
# Using tuple(), data type conversion [tuple + list]
# initializing list
test_list = [5, 6, 7]
# printing original list
print("The original list is : " + str(test_list))
# initializing tuple
test_tup = (9, 10)
# Adding Tuple to List and vice - versa
# Using tuple(), data type conversion [tuple + list]
res = tuple(list(test_tup) + test_list)
# printing result
print("The container after addition : " + str(res))
OutputThe original list is : [5, 6, 7]
The container after addition : (9, 10, 5, 6, 7)
Time complexity: O(1) – the code has a constant runtime that is not dependent on the size of the input.
Auxiliary space: O(n) – the space complexity of the code is dependent on the size of the input list.
Method #3: Using tuple(),list() and extend() methods
Python3
# Python3 code to demonstrate working of
# Adding Tuple to List and vice - versa
# initializing list and tuple
test_list = [5, 6, 7]
test_tup = (9,10)
# printing original list
print("The original list is : " + str(test_list))
# Adding Tuple to List
test_list.extend(list(test_tup))
# printing result
print("The container after addition : " + str(test_list))
#*********************************************************
# initializing list and tuple
test_list = [1,2,3,4]
test_tup=(5,6)
# printing original tuple
print("The original tuple is : " + str(test_tup))
#Adding list to tuple
test_tup=list(test_tup)
test_tup.extend(test_list)
test_tup=tuple(test_tup)
# printing result
print("The container after addition : " + str(test_tup))
OutputThe original list is : [5, 6, 7]
The container after addition : [5, 6, 7, 9, 10]
The original tuple is : (5, 6)
The container after addition : (5, 6, 1, 2, 3, 4)
Time complexity: O(n), where n is the size of the tuple being added.
Auxiliary space: O(n), where n is the size of the tuple being added.
METHOD 4:Using the insert() method: The given code defines a list my_list and a tuple my_tuple. It then inserts the tuple at index 3 of the list using the insert() method and prints the modified list.
- Define a list my_list with elements 5, 6, and 7.
- Define a tuple my_tuple with elements 9 and 10.
- Define a variable index and set it to 3.
- Call the insert() method on my_list and pass index and my_tuple as arguments.
- Print the modified list.
Python3
my_list = [5, 6, 7]
my_tuple = (9, 10)
index = 3
my_list.insert(index, my_tuple)
print("List after addition: ", my_list)
OutputList after addition: [5, 6, 7, (9, 10)]
Time Complexity: O(n)
Space Complexity: O(1)
Method 5:Using zip and sum
The code defines a list original_list with elements [5, 6, 7] and a tuple tuple_to_add with elements (9, 10). It combines elements from both collections in two ways:
- Adding Tuple to List: It pairs elements from original_list and tuple_to_add using zip, flattens the paired elements into a single sequence, and converts this sequence back into a list named list_with_tuple.
- Adding List to Tuple: It pairs elements in a similar manner but converts the flattened sequence into a tuple named tuple_with_list.
- Displaying Results: It prints original_list, list_with_tuple, and tuple_with_list to show the outcomes of these operations.
Python
original_list = [5, 6, 7]
tuple_to_add = (9, 10)
# Adding tuple to list
list_with_tuple = list(sum(zip(original_list, tuple_to_add), ()))
# Adding list to tuple
tuple_with_list = tuple(sum(zip(original_list, list(tuple_to_add)), ()))
# Displaying the results
print("The original list is:", original_list)
print("List with added tuple:", list_with_tuple)
print("Tuple with added list:", tuple_with_list)
Output('The original list is:', [5, 6, 7])
('List with added tuple:', [5, 9, 6, 10])
('Tuple with added list:', (5, 9, 6, 10))
Time Complexity: O(n)
Space Complexity: O(n)
Please Login to comment...