- 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 – Rows with K string in Matrix
When it is required to find rows, which have ‘K’ string in matrix, the ‘enumerate’ attribute, a simple iteration and ‘append’ method is used.
Below is a demonstration of the same −
my_list = [["Pyt", "fun", "python"], ["python", "rock"],["Pyt", "for", "CS"], ["Keep", "learning"]] print("The list is :") print(my_list) K = "Pyt" my_result =  for idx, element in enumerate(my_list): if K in element: my_result.append(idx) print("The result is :") print(my_result)
The list is : [['Pyt', 'fun', 'python'], ['python', 'rock'], ['Pyt', 'for', 'CS'], ['Keep', 'learning']] The result is : [0, 2]
A list is defined and displayed on the console.
The value for K is defined.
An empty list is created.
The list is iterated over using the ‘enumerate’ attribute.
A condition is placed, which checks if ‘K’ is present as one of the elements of the list.
If yes, its index is appended to the empty list.
This is the output that is displayed on the console.
- Python – Filter rows with Elements as Multiple of K
- Python – Convert String to matrix having K characters per row
- Python – Check Similar elements in Matrix rows
- Python Program – Convert String to matrix having K characters per row
- Python – Convert Integer Matrix to String Matrix
- Python – Remove rows with Numbers
- Python – Test if all rows contain any common element with other Matrix
- Python – Mapping Matrix with Dictionary
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Python – Filter immutable rows representing Dictionary Keys from Matrix
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Rows with all List elements
- Python – Sort Matrix by Maximum String Length
- Python – Extract String elements from Mixed Matrix