Check if list is in list of list

python check if list contains elements of another list
check if list is subset of another list python
python check if list contains another list
python check if any element in list is in another list
python find index in list of lists
check if element is present in list of lists
python check if value exists in list
check if value is in list of list python

I have the following to check if list is in list of list. It's returning wrong result. What's the issue in this?

my_list =  [['1', '2', '3'], ['7','8','9','10'], ['1','3','4','5'], ['10','11','12','13']]

w = ['1', '2', '3'] -> True

a = ['1','2'] -> True

b = ['7','8','9','10'] -> True

f = ['1', '0']  -> False

c = [['1','3','4','5'], ['10','11','12','13']]  -> True

d = [['1','3','4','4'], ['10','11','12','15']]  -> False

def x_in_y(search_list, my_list):
    return any([s in my_list for s in search_list])

print x_in_y(c, my_list)
print x_in_y(a, my_list)

partial match should be correct as well as long as all elements in list are present in my_list.

you can directly check for containment of a list into a another list using in:

return search_list in my_list

but since you want also to get the partial containment match and even a list of a list containment, you can try this:

def x_in_y(search_list, my_list):
    if any(isinstance(i, list) for i in search_list):
        return all([x_in_y(x,my_list) for x in search_list])
    return any([m for m in my_list if m == search_list or set(search_list) <= set(m)])

this will check for list matches within list, a nested list containment, as well as subset of a list within list.

Python - Check if list of lists of lists contains a specific list, Just use a containment test: if redList in totalList: This returns True for your sample data: >>> totalList = [ [[0,1], [2,7], [6,3]], [[2,3], [6,1], [4,1]] ]� Besides the Find and Replace function, you can use a formula to check if a value is in a list. Select a blank cell, here is C2, and type this formula =IF(ISNUMBER(MATCH(B2,A:A,0)),1,0) into it, and press Enter key to get the result, and if it displays 1, indicates the value is in the list, and if 0, that is not exist.

You can use the in keyword to check if your list is present:

def x_in_y(search_list, my_list):
    return True if search_list in my_list else False

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'� Now let’s check if given list contains a string element ‘at’ , Check if element exists in list using python “in” Operator. Condition to check if element is in List : elem in LIST It will return True, if element exists in list else return false. For example check if ‘at’ exists in list i.e.

You need to handle the one dimensional case separately

def x_in_y(search_list, my_list):
    return (search_list in my_list) or any([s in my_list for s in search_list])

Python: Check if a list or list of lists is empty or not –, If we apply 'not' operator along with 'if statement' to the list object, then it evaluates to True if list is empty else returns False. Let's see an example� METHOD 2 (By reversing the list) This method takes O(n) time and O(1) extra space. 1) Get the middle of the linked list. 2) Reverse the second half of the linked list. 3) Check if the first half and second half are identical. 4) Construct the original linked list by reversing the second half again and attaching it back to the first half

This code although more lines than the list comprehension, in some cases it will be faster because it breaks once any item is not found.

def x_in_y(search_list, my_list):
    for num in search_list:
        for lst in my_list:
            if num in lst or num==lst:
        if found==False:
            return False
    return True

Check if Python List Contains Elements of Another List, Sample program to check if a Python list contains all or any elements of another list and show the result using the print() function. Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.


search_list = [['1', '2', '3'], ['7','8','9','10'], 
               ['1','3','4','5'], ['10','11','12','13']]

w = ['1', '2', '3']  # True
a = ['1', '2']  # True
b = ['7', '8', '9', '10']  # True
f = ['1', '0']  # False
c = [['1', '3', '4', '5'], ['10', '11', '12', '13']]  # True
d = [['1', '3', '4', '4'], ['10', '11', '12', '15']]  # False

Function: - using generator comprehension instead of list comprehension - code below needs hashable inputs

def x_in_y(x, y):
    # check if x is a nested list
    if any(isinstance(i, list) for i in x):
        return x, all((any((set(x_).issubset(y_) for y_ in y)) for x_ in x))
        return x, any((set(x).issubset(y_) for y_ in y))

all() : Return True if all elements of the iterable are true (or if the iterable is empty) any() : Return True if any element of the iterable is true. If the iterable is empty, return False isinstance : Return true if the each item in x is an instance of the list issubset : Test whether every element in the set is in argument set


print(x_in_y(w, search_list))
## (['1', '2', '3'], True)
print(x_in_y(a, search_list))
## (['1', '2'], True)
print(x_in_y(b, search_list))
## (['7', '8', '9', '10'], True)
print(x_in_y(f, search_list))
## (['1', '0'], False)
print(x_in_y(c, search_list))
## ([['1', '3', '4', '5'], ['10', '11', '12', '13']], True)
print(x_in_y(d, search_list))
## ([['1', '3', '4', '4'], ['10', '11', '12', '15']], False)

Python Check If List Item Exists, Check If List Item Exists. To determine if a specified item is present in a list use the in keyword: Example. Check if "apple" is present in the list: thislist = ["apple"� Here is a sample to find if there are match elements in another list. List<int> nums1 = new List<int> { 2, 4, 6, 8, 10 }; List<int> nums2 = new List<int> { 1, 3, 6, 9, 12}; if (nums1.Any(x => nums2.Any(y => y == x))) { Console.WriteLine("There are equal elements"); } else { Console.WriteLine("No Match Found!"); }

How to check if a list is a subset of another list in Python, A list is a subset of another list if all elements of the former are also elements of the latter. For example, [1, 2] is a subset of [1, 2, 3] , but [1,4]� This list is written by Peggy McIntosh, associate director of the Wellesley Collage Center for Research on Women. From McIntosh's Essay "White Privilege and Male Privilege: A Personal Account of Coming to See Correspondences Through Work in Women's Studies", this list is what she calls the "Daily effects of white privilege."

Python - Check if all elements in a List are same, Sometimes we come across the need to check if we have one single value repeated in a list as list elements. We can check for such scenario� List.Contains. 7/31/2019; 2 minutes to read; In this article Syntax List.Contains(list as list, value as any, optional equationCriteria as any) as logical About. Indicates whether the list list contains the value value. Returns true if value is found in the list, false otherwise.

Check if list contains a value, in Scheme, Check if list contains a value x. list is an iterable finite container. This is a custom implementation, use it if no built-in predicate is available in your Scheme � Console.WriteLine() For Each aPart As Part In parts Console.WriteLine(aPart) Next ' Check the list for part #1734. This calls the IEquatable.Equals method ' of the Part class, which checks the PartId for equality.

  • Your looping over elements of search_list, so presumably that should be a list of lists too? But you're only passing a simple list w as the argument
  • ... for s in search_list means you're looping over the individual elements of search_list, meaning in your example, you're looking for '1', and that value by itself is not a member of your master list.
  • Do counts of the items matter? E.g., does the following test case pass or fail? [['1','3','4','4'], ['10','11','12']]. Also, does the order matter, and if so does relative order suffice?
  • Moderator Note: Please don't vandalize your posts. Once you post a question, they belong to the site and its users. Even if it is no longer useful to you, it might still be beneficial to someone in the future. If there is any confidential information revealed in your post, please edit it out with dummy data, and flag for the history to be redacted.
  • print x_in_y(a, my_list) and print x_in_y(c, my_list) show wrong result
  • why a must be True if the list value of a is not really on my_list?
  • I'm looking for partial match as well
  • it's returning true for partial match for list c, but failing for d. It's returning true even though 15 is in there.
  • @yomangz1 please check my edits. that function will check even nested list
  • print x_in_y(w, my_list) is returning false when it should be true
  • That's desired outcome
  • The same test cases that break @mark's code apply here (though for different reasons). Run a=[];a.append(a);x_in_y(a,a) to get an unhashable type error.
  • @Hans the OP indicated Mark's code fails for "d" nested list by returning true. the code above correctly return's false. it meets all test cases provided by the OP. I am sure one could find few other cases that will break the code but I don't think that is the intent here.