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
test_list
=
[(
5
,
6
), (
2
,
4
), (
5
,
7
), (
2
,
5
)]
print
(
"The original list is : "
+
str
(test_list))
sub_list
=
[
7
,
2
,
4
,
6
]
res
=
[sub
+
tuple
(sub_list)
for
sub
in
test_list]
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
test_list
=
[(
5
,
6
), (
2
,
4
), (
5
,
7
), (
2
,
5
)]
print
(
"The original list is : "
+
str
(test_list))
sub_list
=
[
7
,
2
,
4
,
6
]
res
=
[(
*
sub,
*
sub_list)
for
sub
in
test_list]
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
=
[]
for
tup
in
test_list:
`` new_tup
=
()
`` for
ele
in
tup:
`` new_tup
+
=
(ele,)
`` for
ele
in
sub_list:
`` new_tup
+
=
(ele,)
`` res.append(new_tup)
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.