
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- Remove similar element rows in tuple Matrix in Python
- Python – Find Kth Even Element
- Python – Test if Rows have Similar frequency
- Python – Check Similar elements in Matrix rows
- Python – Remove rows with Numbers
- Python – Extract Kth element of every Nth tuple in List
- Python – Filter Similar Case Strings
- Python – Cross Join every Kth segment
- Python - Remove positional rows
- Python program to remove rows with duplicate element in Matrix
- 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 – Filter Sorted Rows
Advertisements