
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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 bitwise AND of any subset is power of two in Python
- Check One Array is Subset of Another Array in Java
- Check if characters of one string can be swapped to form other in Python
- Python - Check if a list is contained in another list
- Check if list is strictly increasing in Python
- Check if max occurring character of one string appears same no. of times in other in Python
- Check if list is sorted or not in Python
- Program to check whether one tree is subtree of other or not in Python
- Program to check if we reverse sublist of one list to form second list 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 a list exists in given list of lists in Python
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- How to check if a list is empty in Python?
