
- 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 – Rows with all List elements
When it is required to give the rows with all list elements, a flag value, a simple iteration and the ‘append’ method is used.
Example
Below is a demonstration of the same
my_list = [[8, 6, 3, 2], [1, 6], [2, 1,7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True for element in sub_list: if element not in row: flag = False if flag: result.append(row) print("The resultant list is :") print(result)
Output
The list is : [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] The resultant list is : [[2, 1, 7], [8, 1, 2]]
Explanation
A list of list is defined and is displayed on the console.
Another list with integer values is defined.
Another empty list is defined.
The list of list is iterated over and a flag value is set to ‘True’.
If the element present in the integer list is not present in the list, the flag value is set to ‘False’.
In the end, depending on the flag value, the output is determined.
If the value of flag is ‘True’, the element is appended to the empty list.
This is displayed as output on the console.
- Related Questions & Answers
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Combine list with other list elements
- Python – Filter tuple with all same elements
- Python – Strings with all given List characters
- Python – Filter rows with Elements as Multiple of K
- Python Program – Strings with all given List characters
- Python – Adjacent elements in List
- Python – Check Similar elements in Matrix rows
- Python – Filter rows with only Alphabets from List of Lists
- Python – Remove rows with Numbers
- Python – Restrict Elements Frequency in List
- Python – List Elements Grouping in Matrix
- Python – Test if all rows contain any common element with other Matrix
- Python – Elements with same index
Advertisements