Answered by : rohit-bisht
## checking any elment of list_B in list_A
list_A = [1, 2, 3, 4]
list_B = [2, 3, 6]
check = any(item in list_A for item in list_B)
print(check)
# True
Source : | Last Update : Sun, 24 May 20
Answered by : rohit-bisht
## checking all elements of list_B in list_A
list_A = [1, 2, 3, 4]
list_B = [2, 3]
check = all(item in list_A for item in list_B)
print(check)
# True
Source : | Last Update : Sun, 24 May 20
Answered by : grumpy-grasshopper-7tiyrafvhepx
>>> items = set([-1, 0, 1, 2])
>>> set([1, 2]).issubset(items)
True
>>> set([1, 3]).issubset(items)
False
Source : https://stackoverflow.com/questions/3847386/how-to-test-if-a-list-contains-another-list | Last Update : Fri, 23 Apr 21
Answered by : rohit-bisht
## using set
list_A = [1, 2, 3, 4]
list_B = [2, 3]
set_A = set(list_A)
set_B = set(list_B)
print(set_A.intersection(set_B))
# True if there is any element same
# False if there is no element same
Source : | Last Update : Sun, 24 May 20
Answered by : fair-finch-ukx50fa04idx
''' 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")
Source : | Last Update : Thu, 02 Jul 20
Answered by : plain-peacock-4y3jho80jide
##Taking examples of two python lists.
##Take examples of two lists.
list1 = [2,4,0,7,6]
list2 = [1,0,9,7,6]
##the statement for condition is.
check = any(element in list2 for element in list1)
Source : | Last Update : Wed, 29 Jul 20
Answered by : lanya
fruits1 = ['Mango','orange','apple','jackfruit']
fruits2 = ['Mango','orange','apple','jackfruit']
new_list= all(item in fruits1 for item in fruits2)
if new_list is True: print("True")
else : print("False")
Source : https://pythonguides.com/check-if-a-list-exists-in-another-list-python/ | Last Update : Wed, 20 Jul 22
Answered by : rohit-bisht
## using set
list_A = [1, 2, 3, 4]
list_B = [5,1]
set_A = set(list_A)
set_B = set(list_B)
output = False if (set_A.intersection(set_B) == set()) else True
print(output)
# True if there is any element same
# False if there is no element same
Source : | Last Update : Sun, 24 May 20
Answered by : phucukko
// You could use a nested Any() for this check which is available on any Enumerable:
bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));
// Faster performing on larger collections would be to project parameters to source and
// then use Intersect which internally uses a HashSet<T> so instead of O(n^2) for the
// first approach (the equivalent of two nested loops) you can do the check in O(n) :
bool hasMatch = parameters.Select(x => x.source) .Intersect(myStrings) .Any();
// source: https://stackoverflow.com/a/11092955
Source : https://stackoverflow.com/questions/11092930/check-if-listt-contains-any-of-another-list | Last Update : Fri, 08 Jul 22
Answered by :
boolean var = lis1.stream().anyMatch(element -> list2.contains(element));
Source : https://stackoverflow.com/questions/11796371/check-if-one-list-contains-element-from-the-other | Last Update : Wed, 21 Sep 22