Python - Add list elements to tuples list
Last Updated :
08 May, 2023
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be done.
Input : test_list = [(5, 6), (2, 4), (5, 7), (2, 5)], sub_list = [5, 4]
Output : [(5, 6, 5, 4), (2, 4, 5, 4), (5, 7, 5, 4), (2, 5, 5, 4)]
Input : test_list = [(5, 6), (2, 4), (5, 7), (2, 5)], sub_list = [5]
Output : [(5, 6, 5), (2, 4, 5), (5, 7, 5), (2, 5, 5)]
Method #1 : Using list comprehension + "+" operator
The combination of above functionalities can be used to solve this problem. In this, we perform task of adding tuple to list using "+" operator and iteration over all tuples is done using list comprehension.
Python3
# Python3 code to demonstrate working of
# Add list elements to tuples list
# Using list comprehension + "+" operator
# initializing list
test_list = [(5, 6), (2, 4), (5, 7), (2, 5)]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [7, 2, 4, 6]
# Add list elements to tuples list
# Using list comprehension + "+" operator
res = [sub + tuple(sub_list) for sub in test_list]
# printing result
print("The modified list : " + str(res))
Output : The original list is : [(5, 6), (2, 4), (5, 7), (2, 5)]
The modified list : [(5, 6, 7, 2, 4, 6), (2, 4, 7, 2, 4, 6), (5, 7, 7, 2, 4, 6), (2, 5, 7, 2, 4, 6)]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), as it creates a new list res of the same length as the input list test_list. Additionally, it also creates a new list sub_list of length m (the length of the input list sub_list), but this is not dependent on the size of the input and is therefore considered constant space.
Method #2 : Using list comprehension + "*" operator
The combination of above functions can be used to solve this problem. In this, we perform the task of adding list to a tuple using pack-unpack operator "*". This is more efficient method than above method.
Python3
# Python3 code to demonstrate working of
# Add list elements to tuples list
# Using list comprehension + "*" operator
# initializing list
test_list = [(5, 6), (2, 4), (5, 7), (2, 5)]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [7, 2, 4, 6]
# Add list elements to tuples list
# Using list comprehension + "*" operator
res = [(*sub, *sub_list) for sub in test_list]
# printing result
print("The modified list : " + str(res))
Output : The original list is : [(5, 6), (2, 4), (5, 7), (2, 5)]
The modified list : [(5, 6, 7, 2, 4, 6), (2, 4, 7, 2, 4, 6), (5, 7, 7, 2, 4, 6), (2, 5, 7, 2, 4, 6)]
Time complexity: O(n), where n is the length of the input list 'test_list'.
Auxiliary space: O(n), where n is the length of the input list 'test_list'.
Method 3 : using a nested for loop.
- Initialize a list of tuples named test_list with elements (5, 6), (2, 4), (5, 7), (2, 5).
- Initialize a list named sub_list with elements 7, 2, 4, 6.
- Initialize an empty list named res.
- Start a for loop that iterates over each tuple in test_list.
- For each tuple in test_list, create a new empty tuple named new_tup.
- Start a nested for loop that iterates over each element in the current tuple.
- For each element in the current tuple, append it to new_tup.
- Start another nested for loop that iterates over each element in sub_list.
- For each element in sub_list, append it to new_tup.
- After all elements have been appended to new_tup, append the tuple to res.
- After all tuples in test_list have been processed, print the modified list by converting res to a string and concatenating it to a string "The modified list : ".
- End the program.
Python3
test_list = [(5, 6), (2, 4), (5, 7), (2, 5)]
sub_list = [7, 2, 4, 6]
res = []
# Loop through each tuple in the test_list
for tup in test_list:
new_tup = ()
# Loop through each element in the tuple and add it to the new tuple
for ele in tup:
new_tup += (ele,)
# Add the sub_list to the new tuple
for ele in sub_list:
new_tup += (ele,)
# Append the new tuple to the result list
res.append(new_tup)
# Print the result
print("The modified list : " + str(res))
OutputThe modified list : [(5, 6, 7, 2, 4, 6), (2, 4, 7, 2, 4, 6), (5, 7, 7, 2, 4, 6), (2, 5, 7, 2, 4, 6)]
The time complexity of this approach is O(n^2), where n is the length of the test_list.
The auxiliary space complexity is O(n), as we need to store the modified tuples in a new list.
METHOD 4: Using map(), lambda function, and tuple concatenation
APPROACH:
This program takes a list of tuples as input and adds a new set of elements to each tuple. Then it returns the modified list of tuples as output.
ALGORITHM:
1. Define the original list of tuples.
2. Define the modified list as an empty list.
3. Iterate through each tuple in the original list.
4. Convert each tuple to a list.
5. Append the new set of elements to the list.
6. Convert the modified list back to a tuple.
7. Append the modified tuple to the modified list.
8. Return the modified list.
Python3
original_list = [(5, 6), (2, 4), (5, 7), (2, 5)]
modified_list = list(map(lambda x: tuple(list(x) + [7, 2, 4, 6]), original_list))
print("The modified list:", modified_list)
OutputThe modified list: [(5, 6, 7, 2, 4, 6), (2, 4, 7, 2, 4, 6), (5, 7, 7, 2, 4, 6), (2, 5, 7, 2, 4, 6)]
Time complexity: O(n), where n is the length of the original list.
Space complexity: O(n), where n is the length of the original list.
Similar Reads
Python - Add Custom Column to Tuple list Sometimes, while working with Python records, we can have a problem in which we need to add custom column to tuples list. This kind of problem can have application in data domains such as web development. Lets discuss certain ways in which this task can be performed. Input : test_list = [(3, ), (7,
5 min read
Python | Adding N to Kth tuple element Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let's discuss certain ways in which N can be added to Kth element of tuple in list. Method #1 : Using loop Using loops this task c
3 min read
Python | Add tuple to front of list Sometimes, while working with Python list, we can have a problem in which we need to add a new tuple to existing list. Append at rear is usually easier than addition at front. Let's discuss certain ways in which this task can be performed. Method #1 : Using insert() This is one of the way in which t
7 min read
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Update Each Element in Tuple List - Python The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read