Python if item in list is in another list

Python : Check if a list contains all the elements of another list

In this article we will discuss if a list contains all or any elements of another list.

Suppose we have have two list i.e.

# List of string list1 = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # List of string list2 = ['there' , 'hello', 'Hi']

Check if list1 contains all elements of list2 using all()

''' check if list1 contains all elements in list2 ''' result = all(elem in list1 for elem in list2) if result: print("Yes, list1 contains all elements in list2") else : print("No, list1 does not contains all elements in list2"

Python all() function checks if all Elements of given Iterable is True. So, convert the list2 to Iterable and for each element in Iterable i.e. list2 check if element exists in list1.

Читайте также:  Css selector by order

Check if list1 contains any elements of list2 using any()

''' check if list1 contains any elements of list2 ''' result = any(elem in list1 for elem in list2) if result: print("Yes, list1 contains any elements of list2") else : print("No, list1 contains any elements of list2")

Python any() function checks if any Element of given Iterable is True. So, convert the list2 to Iterable and for each element in Iterable i.e. list2 check if any element exists in list1.

Frequently Asked:

Complete example is as follows,

def main(): # List of string list1 = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # List of string list2 = ['there' , 'hello', 'Hi'] ''' check if list1 contains all elements in list2 ''' result = all(elem in list1 for elem in list2) if result: print("Yes, list1 contains all elements in list2") else : print("No, list1 does not contains all elements in list2") ''' check if list1 contains any elements of list2 ''' result = any(elem in list1 for elem in list2) if result: print("Yes, list1 contains any elements of list2") else : print("No, list1 contains any elements of list2") if __name__ == '__main__': main()
Yes, list1 contains all elements in list2 Yes, list1 contains any elements of list2

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Читайте также:  Margin Left

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Check if any element of List is in another List in Python

This tutorial will discuss about a unique way to check if any element of list is in another list in Python.

Suppose we have two lists,

strList1 = ['Sample', 'Like', 'This'] strList2 = ['That', 'Where', 'Why', 'This', 'Now']

Now we want to check if any element from first list i.e. strList1 exists in the second list i.e. strList2 or not.

For this, we can iterate over all the elements of first list, and during iteration, for each element check if it exists in the second list or not. If yes, then break the loop, and mark that an element from first list exists in the second list. We have created a function for this,

def is_overlapped(firstList, secondList): ''' Returns True, if any element from first list exists in the second List.''' result = False for item in firstList: if item in secondList: result = True return result

It accepts two lists as arguments, and retruns True, if any element from first list exists in the second List. Inside the function we will loop over all the elements of first list and check for their existence in the second list.

Frequently Asked:

Example 1

In this example, the first list had an element that exists in the second list.

Let’s see the complete example,

def is_overlapped(firstList, secondList): ''' Returns True, if any element from first list exists in the second List.''' result = False for item in firstList: if item in secondList: result = True return result # Example 1 strList1 = ['Sample', 'Like', 'This'] strList2 = ['That', 'Where', 'Why', 'This', 'Now'] if is_overlapped(strList1, strList2): print('Yes, an element of first List is in second List') else: print('None of the element from first List is not in second List')
Yes, an element of first List is in second List

Example 2

In this example, the first list and second list has no common elements.

Let’s see the complete example,

def is_overlapped(firstList, secondList): ''' Returns True, if any element from first list exists in the second List.''' result = False for item in firstList: if item in secondList: result = True return result strList1 = ['Sample', 'Like', 'Text'] strList2 = ['That', 'Where', 'Why', 'This', 'Now'] if is_overlapped(strList1, strList2): print('Yes, an element of first List is in second List') else: print('None of the element from first List is not in second List')
None of the element from first List is not in second List

Summary

We learned how to check if any element of a List is present in another List in Python. Thanks.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

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.

Источник

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