Given List of tuples, find index-wise absolute difference of consecutive tuples.
Input : test_list = [(5, 3), (1, 4), (9, 5), (3, 5)]
Output : [(4, 1), (7, 1), (6, 0)]
Explanation : 5 – 1 = 4, 4 – 3 = 1. hence (4, 1) and so on.
Input : test_list = [(9, 5), (3, 5)]
Output : [(6, 0)]
Explanation : 9 – 3 = 6, 5 – 5 = 0.
Method #1 : Using list comprehension
This is one way in which this task can be performed. In this, we employ brute force strategy on list using list comprehension strategy to perform this task as a one-liner.
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)] The extracted list : [(5, 1), (7, 1), (5, 0)]
Method #2 : Using tuple() + map() + sub
The combination of above functions can be used to solve this problem. In this, we perform the task of perform subtraction using sub and map() is used to extend the logic to whole list elements. Doesn’t output absolute result.
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)] The extracted list : [(-5, 1), (7, 1), (-5, 0)]
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Remove Consecutive tuple according to key
- Python - Concatenate consecutive elements in Tuple
- Python | Find Maximum difference between tuple pairs
- Python | Sort tuple list on basis of difference of elements
- Python - Get minimum difference in Tuple pair
- Python | Consecutive Character Maximum difference
- Python - K difference Consecutive elements
- Python | Count the elements in a list until an element is a Tuple
- Python | Pair and combine nested list to tuple list
- Python | Convert String to tuple list
- Python - Tuple key detection from value list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Join tuple elements in a 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 firstname.lastname@example.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.