# 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.")
python check all elements in list are in another list
check = all ( item in first_list for item in second_list )
# check = True - All elements from first_list are in second_list
# check = False - Not all elements from first_list are not in second_list
check if a list contains any item from another list python
## 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
'''
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")
how to check if a list contains elements in another list
##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)
check if a list contains any item from another list python
## 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
Python check if all elements exist in another list
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")
python check if any elements in a list are different
# Basic syntax:
if your_list.count(your_list[0]) == len(your_list) # or:
if len(set(your_list)) == 1
# Example usage 1:
your_list = [1,1,1,1,1,1]
if your_list.count(your_list[0]) == len(your_list):
print("All elements are identical")
else:
print("Not all elements are identical")
--> All elements are identical
# Example usage 2:
your_list = [1,1,1,2,1,1]
if len(set(your_list)) == 1:
print("All elements are identical")
else:
print("Not all elements are identical")
--> Not all elements are identical
// 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