
- 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 – Remove Rows for similar Kth column element
When it is required to remove rows for a similar ‘K’th column element, a simple iteration, and the ‘append’ method are used.
Example
Below is a demonstration of the same −
my_list = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67,85,12], [45,65,0]] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [] my_mem = [] for index in my_list: if not index[K] in my_mem: my_result.append(index) my_mem.append(index[K]) print("The resultant list is : ") print(my_result)
Output
The list is : [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] The value of K is 1 The resultant list is : [[45, 95, 26], [70, 35, 74], [87, 65, 23], [67, 85, 12]]
Explanation
A list of list is defined and is displayed on the console.
The value for K is initialized and is printed on the console.
Two empty lists are defined.
The original list is iterated over if a specific index is not found in the second list, the index is appended to first list, and the element at the index is appended to the second list.
The first list is displayed as the output on the console.
- Related Articles
- Remove similar element rows in tuple Matrix in Python
- Python – Find Kth Even Element
- Python program to remove rows with duplicate element in Matrix
- Python - Remove positional rows
- Python – Test if Rows have Similar frequency
- Python – Check Similar elements in Matrix rows
- Kth Column Product in Tuple List in Python
- Kth Largest Element in an Array in Python
- Kth Smallest Element in a BST in Python
- Kth Largest Element in a Stream in Python
- Python – Remove rows with Numbers
- Kth Smallest Element in a Sorted Matrix in Python
- Filter Tuples by Kth element from List in Python
- Remove Element in Python
- Closest Pair to Kth index element in Tuple using Python

Advertisements