- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 – Extract Paired Rows
When it is required to extract paired rows, a list comprehension and the ‘all’ operator is used.
Below is a demonstration of the same
my_list = [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(row.count(element) % 2 == 0 for element in row)] print("The result is :") print(my_result)
The list is : [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] The result is : [[30, 30, 51, 51]]
A list of list is defined and is displayed on the console.
The list comprehension is used to iterate over the elements.
The ‘all’ operator is used to get the ‘count’ of element, and check if it is divisible by 2.
If yes, it is converted to a list, and is assigned to a variable.
This is displayed as output on the console.
- Python – Extract Particular data type rows
- Python – Extract rows with Even length strings
- Python – Extract rows with Complex data types
- Python program to extract rows with common difference elements
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python program to extract rows from Matrix that has distinct data types
- Python – Extract Sorted Strings
- How to extract unique combination of rows in an R data frame?
- Extract digits from Tuple list Python
- Python Extract specific keys from dictionary?
- Extract hyperlinks from PDF in Python
- Python – Extract Percentages from String
- Python - Remove positional rows
- How to perform paired t test for multiple columns in R?
- How to extract unique rows by categorical column of a data.table object in R?