- 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 - Print rows from the matrix that have same element at a given index
When it is required to print the rows from the matrix that have the same element at the given index, a list comprehension and the ‘all’ operator is used.
Below is a demonstration of the same −
my_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] print("The list is :") print(my_list) my_key = 1 print("The key is ") print(my_key) my_result = [element for element in my_list if all(str(i)[my_key] == str(element)[my_key] for i in element)] print("The result is :") print(my_result)
The list is : [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] The key is 1 The result is : [[7745, 6755, 87, 978], [192, 997, 49]]
A list of list is defined and is displayed on the console.
The value for key is defined and displayed on the console.
A list comprehension is used to iterate over the list, and the ‘all’ operator is used to check if a specific element at an index is equal to the key.
This is converted to a list and is assigned to a variable.
This is displayed as output on the console.
- Python program to return rows that have element at a specified index
- Python Program that prints the rows of a given length from a matrix
- Count rows in a matrix that consist of same element in C++
- Find rows that have the same value on a column in MySQL?
- Get rows that have common value from the same table with different id in MySQL
- How to return rows that have the same column values in MySQL?
- Python Program that print elements common at specified index of list elements
- Python Pandas - Insert a new index value at the first index from the last
- Python program to extract rows from Matrix that has distinct data types
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- Count how many rows have the same value in MySQL?
- Construct an array from XOR of all elements of array except element at same index in C++
- Python Program that filters out non-empty rows of a matrix
- Remove similar element rows in tuple Matrix in Python
- Print all nodes at distance k from a given node in C++