

- 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
Check if one list is subset of other in Python
In text analytics and various other fields of data analytics it is often needed to find if a given list is already a part of a bigger list. In this article we will see the python programs to implement this requirement.
With all
We use a for loop to check if every element of the smaller list is present in the bigger list. The all function ensures each evaluation returns true.
Example
Alist = ['Mon','Tue', 5, 'Sat', 9] Asub_list = ['Tue',5,9] # Given list and sublist print("Given list ",Alist) print("Given sublist",Asub_list) # With all if (all(x in Alist for x in Asub_list)): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list") # Checkign again Asub_list = ['Wed',5,9] print("New sublist",Asub_list) if (all(x in Alist for x in Asub_list)): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list")
Output
Running the above code gives us the following result −
Given list ['Mon', 'Tue', 5, 'Sat', 9] Given sublist ['Tue', 5, 9] Sublist is part of bigger list New sublist ['Wed', 5, 9] Sublist is not part of bigger list
With subset
In this approach we convert the lists into set and use the subset functions to validate if the small list is part of the bigger list or not.
Example
Alist = ['Mon','Tue', 5, 'Sat', 9] Asub_list = ['Tue',5,9] # Given list and sublist print("Given list ",Alist) print("Given sublist",Asub_list) # With all if(set(Asub_list).issubset(set(Alist))): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list") # Checkign again Asub_list = ['Wed',5,9] print("New sublist",Asub_list) if(set(Asub_list).issubset(set(Alist))): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list")
Output
Running the above code gives us the following result −
Given list ['Mon', 'Tue', 5, 'Sat', 9] Given sublist ['Tue', 5, 9] Sublist is part of bigger list New sublist ['Wed', 5, 9] Sublist is not part of bigger list
Using intersection
The intersection function find the common elements between two sets. In this approach we convert the lists into sets and apply the intersection function. If the result of intersection is same as the sublist then we conclude the sublist is part of thelist.
Example
Alist = ['Mon','Tue', 5, 'Sat', 9] Asub_list = ['Tue',5,9] # Given list and sublist print("Given list ",Alist) print("Given sublist",Asub_list) # With all if(set(Alist).intersection(Asub_list)== set(Asub_list)): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list") # Checkign again Asub_list = ['Wed',5,9] print("New sublist",Asub_list) if(set(Alist).intersection(Asub_list)== set(Asub_list)): print("Sublist is part of bigger list") else: print("Sublist is not part of bigger list")
Output
Running the above code gives us the following result −
Given list ['Mon', 'Tue', 5, 'Sat', 9] Given sublist ['Tue', 5, 9] Sublist is part of bigger list New sublist ['Wed', 5, 9] Sublist is not part of bigger list
- Related Questions & Answers
- Check if one tuple is subset of other in Python
- Check if one of the numbers is one’s complement of the other in Python
- Check if characters of one string can be swapped to form other in Python
- Check if bitwise AND of any subset is power of two in Python
- Check if max occurring character of one string appears same no. of times in other in Python
- Program to check whether one tree is subtree of other or not in Python
- Check if list is strictly increasing in Python
- Python - Check if a list is contained in another list
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Check if list is sorted or not in Python
- Program to find length of the largest subset where one element in every pair is divisible by other in Python
- Check if edit distance between two strings is one in Python
- Check if a HashSet is a subset of the specified collection in C#
- Check if a SortedSet is a subset of the specified collection in C#
- Check if strings are rotations of each other or not in Python