
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
Consecutive Nth Column Difference in Tuple List Using Python
When it is required to find the consecutive column difference in a list of tuple, it can be iterated over, and the ‘abs’ method and the ‘append’ method can be used.
The ‘abs’ method returns the absolute or positive value, and the append adds elements to a list.
Below is a demonstration of the same −
Example
my_list = [(67, 89, 32), (11, 23, 44), (65, 75, 88)] print("The list is : ") print(my_list) print("The value of k has been initialized") K = 1 my_result = [] for idx in range(0, len(my_list) - 1): my_result.append(abs(my_list[idx][K] - my_list[idx + 1][K])) print("The resultant list of tuple is : ") print(my_result)
Output
The list is : [(67, 89, 32), (11, 23, 44), (65, 75, 88)] The value of k has been initialized The resultant list of tuple is : [66, 52]
Explanation
A list of tuple is defined, and is displayed on the console.
The value of K is initialized and displayed on the console.
An empty list is defined.
The list of tuple is iterated over, and the difference between the elements is determined.
This difference is added to the empty list.
This is displayed as output on the console.
Advertisements