Open In App

Check if a Nested List is a Subset of Another Nested List – Python

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task is to check if all sublists in one nested list are present in another nested list. This is done by verifying whether each sublist in the second list exists in the first list.

For example, given list1 = [[2, 3, 1], [4, 5], [6, 8]] and list2 = [[4, 5], [6, 8]], we check if both [4, 5] and [6, 8] are in list1, which would return True in this case

Using set()

To check if one nested list is a subset of another, we convert the inner lists into tuples since sets can only contain hashable items and then use set operations. By converting the nested lists into sets of tuples, we can efficiently check if all elements of one list are present in the other using the issubset() method.

Python
a = [[2, 3, 1], [4, 5], [6, 8]]
b = [[4, 5], [6, 8]]

# Convert lists of lists into sets of tuples
x = {tuple(x) for x in a}
y = {tuple(x) for x in b}

# Check if set2 is a subset of set1
res = y.issubset(x)
print(res)

Output
True

Explanation:

  • {tuple(x) for x in a} converts lists in a to tuples in set x .
  • {tuple(x) for x in b} converts lists in b to tuples in set y.
  • y.issubset(x) checks if all elements of y are present in x.

Using all()

all() function efficiently check if all elements of list2 are present in list1. It iterates over list2 and checks for each element in list1. This function stops further checking as soon as a mismatch is found, improving performance.

Python
a = [[2, 3, 1], [4, 5], [6, 8]]
b = [[4, 5], [6, 8]]

res = all(i in a for i in b)
print(res)

Output
True

Explanation: all(i in a for i in b) checks if each sublist in b is present in a and returns True if all elements of b are in a, otherwise False.

Using any()

In this method, we iterate over list2 and use the any() function to check if each sublist is present in list1. This function evaluates whether any sublist in list2 is not found in list1 and stops further checking if a mismatch is found. Although this approach is slightly less efficient than using all().

Python
a = [[2, 3, 1], [4, 5], [6, 8]]
b = [[4, 5], [7, 9]]

# Check if any sublist in `b` is not in `a`
res = not any(sublist not in a for sublist in b)
print(res)

Output
True

Explanation:

  • sublist not in a for sublist in b checks if each sublist from b is not in a.
  • any() returns True if any sublist in b is not in a.
  • not negates the result from any(). If any() returns True, not makes the result False.

Using loop

In this approach, we manually iterate over the elements of list2 and check each sublist’s presence in list1 through a nested loop. This method is less efficient than other techniques like all() function or set() .

Python
a = [[2, 3, 1], [4, 5], [6, 8]]
b = [[4, 5], [6, 8]]

res = True
for sublist in b:
  
     # Initialize found to False for each sublist
    found = False
    
    for i in a:
        if sublist == i:
            found = True
            break
    if not found:
      
       # If a sublist is not found, set res to False
        res = False
        break

print(res)

Output
True

Explanation:

  • (for each sublist in b) iterate through each sublist in b to check its presence in a.
  • (for each sublist in a) compare the current sublist from b with each sublist in a.
  • if sublist == i : If a match is found, set found = True and break the inner loop to stop further comparisons for that sublist.
  • If found is False: If no match is found after checking all elements in a, set res = False and break the outer loop since not all sublists are present.


Next Article

Similar Reads