Python check if one list is in another list

Check if Python List Contains All Elements of Another List

In this sample program, you will learn to check if a Python list contains all the elements of another list and show the result using the print() function.

To understand this demo program, you should have basic Python programming knowledge.

Check if Python List Contains Elements of Another List

In the sample below, we are using two lists having overlapping values. One of these is the big one which holds all the elements of the second one.

  • List1 – This list contains all or some of the elements of another.
  • List2 – It is a subset of the first one.

Now, we’ve to programmatically prove that List1 contains the elements of List2. There could be multiple ways to achieve it.

all() method

To demonstrate that List1 has List2 elements, we’ll use the all() method.

Читайте также:  Html button onclick ссылка

# Program to check the list contains elements of another list # List1 List1 = [‘python’ , ‘javascript’, ‘csharp’, ‘go’, ‘c’, ‘c++’] # List2 List2 = [‘csharp1’ , ‘go’, ‘python’] check = all(item in List1 for item in List2) if check is True: print(«The list <> contains all elements of the list <>«.format(List1, List2)) else : print(«No, List1 doesn’t have all elements of the List2.»)

The output of the above code is as follows:

The list ['python', 'javascript', 'csharp', 'go', 'c', 'c++'] contains all elements of the list ['csharp', 'go', 'python']

any() method

Another method is any() which we can use to check if the list contains any elements of another one.

# Program to check the list contains elements of another list # List1 List1 = ['python' , 'javascript', 'csharp', 'go', 'c', 'c++'] # List2 List2 = ['swift' , 'php', 'python'] check = any(item in List1 for item in List2) if check is True: print("The list <> contains some elements of the list <>".format(List1, List2)) else : print("No, List1 doesn't have any elements of the List2.")

The output of the above code is as follows:

The list [‘python’, ‘javascript’, ‘csharp’, ‘go’, ‘c’, ‘c++’] contains some elements of the list [‘swift’, ‘php’, ‘python’]

In this method, we’ll write a custom search method to test if the first list contains the second one. While iterating the lists if we get an overlapping element, then the function returns true. The search continues until there is no element to match and returns false.

# Program to check if a Python list contains elements of another list def list_contains(List1, List2): check = False # Iterate in the 1st list for m in List1: # Iterate in the 2nd list for n in List2: # if there is a match if m == n: check = True return check return check # Test Case 1 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['x', 'y', 'z', 'l', 'm'] print("Test Case#1 ", list_contains(List1, List2)) # Test Case 2 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['a', 'b', 'c', 'd', 'e'] print("Test Case#2 ", list_contains(List1, List2))

The output of the above code is as follows:

Test Case#1 False Test Case#2 True

set() method

We’ll use the set() method to convert the lists and call Python set intersection() method to find if there is any match between the list elements.

# Program to check if a Python list contains elements of another list def list_contains(List1, List2): set1 = set(List1) set2 = set(List2) if set1.intersection(set2): return True else: return False # Test Case 1 List1 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] List2 = [‘x’, ‘y’, ‘z’, ‘l’, ‘m’] print(«Test Case#1 «, list_contains(List1, List2)) # Test Case 2 List1 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] List2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’] print(«Test Case#2 «, list_contains(List1, List2))

The output of the above code is as follows:

Test Case#1 False Test Case#2 True

Источник

How to check if a list exists in another list in Python

In this Python tutorial, I will show you, how to check if a list exists in another list in Python with examples. I will also show you, how to check if a list contains another list in Python.

Check if a list exists in another list in Python

Now, we can see how to check if a list exists in another list in Python.

  • In this example, I have taken a variable as a list and another variable as a check_list.
  • And if condition is used if the check_list is present in the list then the output will be “List is present”, else “List is not present”.
  • To get the output, I have used print(“List is present”).
 list = [[1,5,7,], [2, 3, 4], [3, 6, 9], [4, 8, 12]] check_list = [2,3,4] if check_list in list: print("List is present") else: print("List is not present") 

We can see the output as List is present. You can refer to the below screenshot for the output.

python check if list is in another list

Another Example: check if a list is in another list in Python

Now, let us check another example of how to check if a list is in another list in Python.

In Python, when you want to check if a list is in another list, you might be checking for two different scenarios: (1) you want to check if all elements of a sublist are within a larger list (but not necessarily consecutively), or (2) you want to check if a sublist appears consecutively within a larger list. Let’s go through both scenarios:

Scenario 1: Check if all elements of a sublist are within a larger list

For this scenario, you might want to make use of Python’s built-in functions and operators. The all() function can be useful here, which returns True if all elements in the iterable are true.

def check_if_sublist_within_list(sublist, larger_list): return all(element in larger_list for element in sublist) # Example: larger_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sublist = [2, 5, 8] result = check_if_sublist_within_list(sublist, larger_list) print(result) # Output: True

This function takes a sublist and a larger_list as input and checks if all the elements of the sublist are within the larger list.

Scenario 2: Check if a sublist appears consecutively within a larger list

To check if the sublist appears consecutively within the larger list, you can iterate through the larger list and check for the occurrence of the sublist.

def check_if_sublist_consecutive(sublist, larger_list): sublist_length = len(sublist) for i in range(len(larger_list) - sublist_length + 1): if larger_list[i:i + sublist_length] == sublist: return True return False # Example: larger_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] sublist = [4, 5, 6] result = check_if_sublist_consecutive(sublist, larger_list) print(result) # Output: True

This function takes a sublist and a larger_list as input and checks if the sublist appears consecutively within the larger list in Python.

How to check if a list contains another list in Python

Now, we can see how to check if a list contains another list in Python.

  • In this example, I have taken a variable as a list, and the if condition is used to check.
  • If the check_list =[“orange”] is present in the list then it returns “List is present” else “List is not present”.
list = [["watermelon"], ["mango"], ["orange"], ["apple"]] check_list = ["orange"] if check_list in list: print("List is present") else: print("List is not present") 

As the check_list is present in the list it returns true as the output. You can refer to the below screenshot for the output.

Check if a list contain another list python

This is how to check if a list contains another list in Python.

You may like the following Python list tutorials:

In this tutorial, we have learned about how to check if a list exists in another list in Python, and also we have covered these topics:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

Python — Check if a list is contained in another list

Given two different python lists we need to find if the first list is a part of the second list.

With map and join

We can first apply the map function to get the elements of the list and then apply the join function to cerate a comma separated list of values. Next we use the in operator to find out if the first list is part of the second list.

Example

listA = ['x', 'y', 't'] listB = ['t', 'z','a','x', 'y', 't'] print("Given listA elemnts: ") print(', '.join(map(str, listA))) print("Given listB elemnts:") print(', '.join(map(str, listB))) res = ', '.join(map(str, listA)) in ', '.join(map(str, listB)) if res: print("List A is part of list B") else: print("List A is not a part of list B")

Output

Running the above code gives us the following result −

Given listA elemnts: x, y, t Given listB elemnts: t, z, a, x, y, t List A is part of list B

With range and len

We can design a for loop to check the presence of elements form one list in another using the range function and the len function.

Example

listA = ['x', 'y', 't'] listB = ['t', 'z','a','x', 'y', 't'] print("Given listA elemnts: \n",listA) print("Given listB elemnts:\n",listB) n = len(listA) res = any(listA == listB[i:i + n] for i in range(len(listB) - n + 1)) if res: print("List A is part of list B") else: print("List A is not a part of list B")

Output

Running the above code gives us the following result −

Given listA elemnts: ['x', 'y', 't'] Given listB elemnts: ['t', 'z', 'a', 'x', 'y', 't'] List A is part of list B

Источник

Оцените статью