Python Add list elements to tuples list (original) (raw)

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.

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))

`

Output

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)]

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)

`

Output

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 original list.
Space complexity: O(n), where n is the length of the original list.