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 perfrom task of adding tuple to list using “+” operator and iteration over all tuples is done using list comprehension.
# 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)) |
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)]
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 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)) |
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)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python | How to Concatenate tuples to nested tuples
- Python | Convert list of tuples to list of list
- Python | Find top K frequent elements from a list of tuples
- Python | Swap tuple elements in list of tuples
- Python | Convert list elements to bi-tuples
- Python program to Convert a elements in a list of Tuples to Float
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of strings
- Python | Update a list of tuples using another list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

