Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding list elements to tuples list in Python
Our task is to add list items to a tuple list (i.e, a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses, as shown below:
Tuples = (11,22,33)
And the list of tuples is represented as follows:
List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)]
Scenario
Suppose we have a list of tuples, "a", and another list of items, "b". We have to add all items of the "b" to each item of the "a" as follows ?
Input Lists: a = [(2,3),(4,5),(6,7)] b = [1, 9] Desired Output: new list = [(2,3,1,9),(4,5,1,9),(6,7,1,9)]
Methods to Add List Elements to Tuples List
We can add list items to a list of tuples in Python using the following approaches:
- Using "+" operator
- Using list comprehension and "+" operator
- Using list comprehension and "*" operator
- Using map() function and "+" operator
Let us go through all these one by one ?
Using "+" Operator
The "+" operator performs a concatenation in Python when used between iterables like lists and tuples. To add a list to a list of tuples, we need to iterate through the list of tuples using a loop and at every iteration ?
- Convert the list to a tuple using "tuple()".
- Add/concatenate the converted tuple to each tuple using the "+" operator.
- Append the result to an empty list using the append() method.
Example
The following program prints the list of tuples after adding another list's items using the "+" operator in Python ?
# Initialising lists with tuples
tup_list = [(2,3),(4,5),(6,7)]
items = [1,2,3]
# Initialising empty result list
result = []
# Iterating over tup_list up to its length
for tup in tup_list:
new_tuple = tup + tuple(items)
result.append(new_tuple)
# Printing the result
print(result)
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Using List Comprehension and "+" Operator
List comprehension is a concise way of creating lists in Python. It performs an operation on each element of a given iterable object and returns the result in the form of a list. Following is the syntax ?
result = [operation for element in sequence]
To add a list to a list of tuples, we need to iterate through it and add the desired list to each element as shown below ?
result = [element + tuple(items) for element in tup_list]
Example
The following program prints the list of tuples after adding items of a list using list comprehension and "+" operator in Python ?
# Initialising lists tup_list = [(2,3),(4,5),(6,7)] items = [1,2,3] # Adding list elements to tuples list # Using list comprehension and "+" operator result = [element + tuple(items) for element in tup_list] # Printing the result print(result)
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Using List Comprehension and "*" Operator
The '*' operator has various functionalities depending on the context in which it is used. If we place it before an iterable object, it serves as an unpacking operator, i.e., it unpacks all the elements of the iterable object.
To add the elements of a list to a list of tuples, we can use this (*) operator within the list comprehension as shown below ?
result = [(*element, *items) for element in tup_list]
This will iterate through each element (tuple) of the given list of tuples. At each iteration ?
- It will extract the elements of the current tuple.
- Extract the elements of the list (that is to be added).
- Creates a new tuple with all the extracted elements and stores it in the resultant list.
Example
The following program prints the list of tuples after adding items of a list using list comprehension and "*" operator in Python ?
# Initialising lists tup_list = [(2,3),(4,5),(6,7)] items = [1,2,3] # Adding list elements to tuples list # Using list comprehension and "*" operator result = [(*element, *items) for element in tup_list] # Printing the result print(result)
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Using map() Function and "+" Operator
The map() function in Python accepts two arguments: a function and an iterable. It applies the given function on each item of the iterable and returns the result as a map object.
To add the elements of a list to a list of tuples using the map() function, we need to pass the following as its arguments ?
- A lambda function that concatenates the elements of the list with the individual tuples using the '+' operator.
- List of tuples as the iterable.
Example
The following program adds the elements of a list to a list of tuples using the map() function and the "+" operator ?
# Initialising lists tup_list = [(2,3),(4,5),(6,7)] items = [1,2,3] # Adding list elements to tuple list # Using map() and "+" operator result = list(map(lambda tup: tup + tuple(items), tup_list)) # Printing the result print(result)
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For loop with "+" operator | Good | Moderate | Simple, readable code |
| List comprehension with "+" | Excellent | Good | Concise one-liner |
| List comprehension with "*" | Good | Best | Memory efficiency |
| map() function | Moderate | Good | Functional programming |
Conclusion
In this article, we have seen multiple approaches for adding elements of a list to a list of tuples in Python. List comprehension with the "*" operator provides the best performance, while list comprehension with "+" operator offers the most readable one-liner solution.
