Sometimes, while working with Python list, we can have a problem in which we need to construct tuples, with the preceding element, whenever that element matches a particular condition. This can have potential application in day-day programming. Let’s discuss a way in which this task can be performed.
Method : Using
zip() + list comprehension
This task can be performed using the combination of above functionalities. In this, the
zip() performs the task of construction of tuples and the catering of condition matching and iteration is handled by list comprehension.
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg'] Tuple list with desired Preceding elements [(4, 'gfg'), (8, 'gfg'), (9, 'gfg')]
- Python | Find the tuples containing the given element from a list of tuples
- Python | Accessing nth element from tuples in list
- Python | Get first element with maximum value in list of tuples
- Python | Filter tuples according to list element presence
- Python - Rear element extraction from list of tuples records
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Convert string tuples to list tuples
- Python | Combining tuples in list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | N element incremental tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.