
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to extract rows with common difference elements
When it is required to extract rows with common difference elements, an iteration and a flag value is used.
Example
Below is a demonstration of the same
my_list = [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, 10]] print("The list is :") print(my_list) my_result = [] for row in my_list: temp = True for index in range(0, len(row) - 1): if row[index + 1] - row[index] != row[1] - row[0]: temp = False break if temp : my_result.append(row) print("The resultant list is :") print(my_result)
Output
The list is : [[31, 27, 10], [8, 11, 12], [11, 12, 13], [6, 9, 10]] The resultant list is : [[11, 12, 13]]
Explanation
A list of tuple is defined and is displayed on the console.
An empty list is created.
The list is iterated over, and a variable is assigned to ‘True’.
The indices are also iterated over.
If the difference between previous index and current index is not equal to the difference between the previous element and current element, the variable is assigned ‘False’.
The control breaks out of it.
In the end, if the variable’s value is ‘True’, the element is appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python program to extract Mono-digit elements
- Python – Extract rows with Even length strings
- Python – Extract rows with Complex data types
- Python – Extract Paired Rows
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- Find distinct elements common to all rows of a matrix in Python
- Python – Extract tuples with elements in Range
- Python program to extract rows from Matrix that has distinct data types
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Rows with all List elements
- Python – Extract elements with equal frequency as value
- Python Program to Extract Strings with a digit
- Python – Extract Particular data type rows

Advertisements