 # Python – Check if one list is subset of other

## Python | Check if one list is subset of other

Sometimes we encounter the problem of checking if one list is just an extension of the list i.e just a superset of one list. This kind of problems are quite popular in competitive programming. Having shorthands for it helps the cause. Lets discuss various ways to achieve this particular task.

Method #1 : Using all()
all() is used to check all the elements of a container in just one line. Checks for all the elements of one list for existence in other list.

 `# Python3 code to demonstrate` `# to check if list is subset of other` `# using all()` `# initializing list` `test_list ``=` `[``9``, ``4``, ``5``, ``8``, ``10``]` `sub_list ``=` `[``10``, ``5``, ``4``]` `# printing original lists` `print` `(``"Original list : "` `+` `str``(test_list))` `print` `(``"Original sub list : "` `+` `str``(sub_list))` `# using all() to` `# check subset of list` `flag ``=` `0` `if``(``all``(x ``in` `test_list ``for` `x ``in` `sub_list)):` `    ``flag ``=` `1` `    ` `# printing result` `if` `(flag) :` `    ``print` `(``"Yes, list is subset of other."``)` `else` `:` `    ``print` `(``"No, list is not subset of other."``)`

Output :

```Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5, 4]
Yes, list is subset of other.```

Method #2 : Using set.issubset()
The most used and recommended method to check for a sublist. This function is tailor made to perform the particular task of checking if one list is a subset of another.

 `# Python3 code to demonstrate` `# to check if list is subset of other` `# using issubset()` `# initializing list` `test_list ``=` `[``9``, ``4``, ``5``, ``8``, ``10``]` `sub_list ``=` `[``10``, ``5``]` `# printing original lists` `print` `(``"Original list : "` `+` `str``(test_list))` `print` `(``"Original sub list : "` `+` `str``(sub_list))` `# using issubset() to` `# check subset of list` `flag ``=` `0` `if``(``set``(sub_list).issubset(``set``(test_list))):` `    ``flag ``=` `1` `    ` `# printing result` `if` `(flag) :` `    ``print` `(``"Yes, list is subset of other."``)` `else` `:` `    ``print` `(``"No, list is not subset of other."``)`

Output :

```Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5]
Yes, list is subset of other.```

Method #3 : Using set.intersection()
Yet another method dealing with sets, this method checks if the intersection of both the lists ends up to be the sub list we are checking. This confirms that one list is a subset of the other.

 `# Python3 code to demonstrate` `# to check if list is subset of other` `# using intersection()` `# initializing list` `test_list ``=` `[``9``, ``4``, ``5``, ``8``, ``10``]` `sub_list ``=` `[``10``, ``5``]` `# printing original lists` `print` `(``"Original list : "` `+` `str``(test_list))` `print` `(``"Original sub list : "` `+` `str``(sub_list))` `# using intersection() to` `# check subset of list` `flag ``=` `0` `if``((``set``(sub_list) & ``set``(test_list))``=``=` `set``(sub_list)):` `    ``flag ``=` `1` `    ` `# printing result` `if` `(flag) :` `    ``print` `(``"Yes, list is subset of other."``)` `else` `:` `    ``print` `(``"No, list is not subset of other."``)`

Output :

```Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5]
Yes, list is subset of other.```

Method #4 : Using iteration and counter
Using count of element in both lists to check whether the second list is a subset of the first list.

 `# Python3 code to demonstrate` `# to check if list is subset of other` `#Importing` `from` `collections ``import` `Counter` `def` `checkInFirst(a, b):` `     ``#getting count` `    ``count_a ``=` `Counter(a)` `    ``count_b ``=` `Counter(b)` `    ``#checking if element exists in second list` `    ``for` `key ``in` `count_b:` `        ``if` `key ``not` `in`  `count_a:` `            ``return` `False` `        ``if` `count_b[key] > count_b[key]:` `            ``return` `False` `    ``return` `True` `# initializing list` `a ``=` `[``1``, ``2``,``4``,``5``]` `b ``=` `[``1``, ``2``,``3``]` `#Calling function` `res ``=` `checkInFirst(a, b)` `#Printing list` `print` `(``"Original list : "` `+` `str``(a))` `print` `(``"Original sub list : "` `+` `str``(b))` `if` `res``=``=``True` `:` `    ``print` `(``"Yes, list is subset of other."``)` `else` `:` `    ``print` `(``"No, list is not subset of other."``)` `    ` `#Added by Paras Jain(everythingispossible)`

Output :

```Original list : [1, 2, 4, 5]
Original sub list : [1, 2, 3]
No, list is not subset of other.```

Method #5 : Using set index

 `# Python3 code to demonstrate` `# to check if list is subset of other` `# initializing list` `one ``=` `[``1``,``2``,``3``,``4``,``5``]` `two ``=` `[``1``,``2``]` `#using set to find if element exists in another list` `result ``=` `set``(x ``in` `one ``for` `x ``in` `two)` `flag ``=` `0` `for` `ans ``in` `result:` `    ``if` `ans ``=``=` `False``:` `        ``flag``=``1` `#Printing list` `print` `(``"Original list : "` `+` `str``(one))` `print` `(``"Original sub list : "` `+` `str``(two))` `if` `flag``=``=``0``:` `    ``print``(``"Yes, list is subset of other."``)` `else``:` `    ``print``(``"No, list is not subset of other."``)` `#Added by Paras Jain(everythingispossible)`

Output :

```Original list : [1, 2, 3, 4, 5]
Original sub list : [1, 2]
Yes, list is subset of other.```

Last Updated on November 13, 2021 by admin

## Python | Binding function in TkinterPython | Binding function in Tkinter

Python | Binding function in Tkinter Tkinter is a GUI (Graphical User Interface) module that is

## Find size of a list in PythonFind size of a list in Python

Find size of a list in Python In Python, List is a collection data-type which

## How to select multiple columns in a pandas dataframeHow to select multiple columns in a pandas dataframe

When working with data in Python using the Pandas library, it is common to need

## Complex Numbers in PythonComplex Numbers in Python

Complex Numbers in Python Not only real numbers, Python can also handle complex numbers and

## Filtering data with Pandas .query() methodFiltering data with Pandas .query() method

Pandas query() method allows for more complex filtering of data using string expressions. Pandas query

## enum in Pythonenum in Python

enum in Python Enumerations in Python are implemented by using the module named “enum“.Enumerations are

## Python range() functionPython range() function

Python range() function Python range() function returns the sequence of the given number between the

## Plot Multiple lines in MatplotlibPlot Multiple lines in Matplotlib

Plot Multiple lines in Matplotlib In this article, we will learn how to plot multiple