
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)]
Adding items to a list of tuples in Python
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 a 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. The append() method of the List object accepts an element and adds it to the end of the current list.
To add a list to a list of tuples, we need to iterate through the list of tuples using a loop at every iteration -
- Convert the list to a tuple using "tuple()".
- Add/concatenate the converted tuple to each tuple (of list of tuples) 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)] list = [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(list) result.append(new_tuple) # Printing the result print(result)
Output
[(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 -
list = [operation for ele in sequence]
To add a list to a list of tuples, we need to iterate through it (list of tuples) and add the desired list to each element as shown below -
res = [ele + tuple(lst) for ele in tup_list]
Here, our sequence will be a list of tuples, and the operation would be "list+ele".
Example
The following program prints the list of tuples after adding items of a list using a list comprehension and "+" operator in Python.
# Initialising lists tup_list = [(2,3),(4,5),(6,7)] list = [1,2,3] # Adding list elements to tuples list # Using list comprehension and "+" operator res = [ele + tuple(list) for ele in tup_list] # Printing the result print(res)
Output
[(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 -
res = [(*ele, *list) for ele in list_of_tuples]
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 a list comprehension and "*" operator in Python.
# Initialising lists tup_list = [(2,3),(4,5),(6,7)] list = [1,2,3] # Adding list elements to tuples list # Using list comprehension and "*" operator res = [(*ele, *list) for ele in tup_list] # Printing the result print(res)
Output
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Using a 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 new tuple).
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 -
- list of tuples.
- A lambda function that concatenates the elements of the list with the individual tuples of the given list of tuples, using the '+' operator.
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)] item_list = [1,2,3] # Adding list elements to tuple list # Using map() and "+" operator res = list(map(lambda tup: tuple(list(tup) + item_list), tup_list)) # Printing the result print(res)
Output
[(2, 3, 1, 2, 3), (4, 5, 1, 2, 3), (6, 7, 1, 2, 3)]
Conclusion
In this article, we have seen multiple approaches for adding elements of a list to a list of tuples in Python. The list comprehension and the map() function are a concise way that allows us to achieve this task using a single line of statement.