>>> L1 = [2,3,4]
>>> L2 = [1,2]
>>> [i for i in L1 if i in L2]
[2]


>>> S1 = set(L1)
>>> S2 = set(L2)
>>> S1.intersection(S2)
set([2])

Both empty lists and empty sets are False, so you can use the value directly as a truth value.

Answer from Joe Koberg on Stack Overflow
🌐
Quora
quora.com › How-do-I-efficiently-find-which-elements-of-a-list-are-in-another-list
How to efficiently find which elements of a list are in another list - Quora
Answer (1 of 5): The obvious way of doing this requires quadratic time—for each element in the first list, walk the entire second list to find a match. If you’re willing to trade space for time, you can reduce this to log-linear or amortized ...
🌐
Reddit
reddit.com › r/haskellquestions › how to check if all elements in one list is in another?
r/haskellquestions on Reddit: How to check if all elements in one list is in another?
January 31, 2023 -

I'm learning Haskell, and I'm stuck on the following problem:

I can do this:

'a' `elem` "Some String"

But I want to do the following:

['a'..'z'] `elem` "Some String"

This doesn't work. I want to check if all the elements of ['a'..'z'] are in the string, but I can't figure out how to express that other than like this:

let string = "Some String"
result =    'a' `elem` string &&
            'b' `elem` string &&
            ...
            'z' `elem` string

How can I do that?

Top answer
1 of 3
8
If you're going to learn Haskell, one of the most fundamental things for you to learn is how to use pattern matching. here, your goal is to write a function these `isSubsetOf` those where these and those are a lists. You want it to be True when every element of these is an element of those. As you pointed out, this is the same as checking elem for every element of these and combining their answers with &&. c0 `elem` those && c1 `elem` those && ... We see that the number of elem checks we need to do depends on what the list these looks like. So, the solution to this problem is to look at the these list. isSubsetOf needs to look at the these list, and it does so by pattern matching. these `isSubsetOf` those = case these of [] -> True c : cs -> (c `elem` those) && (cs `isSubsetOf` those) This code is based on our understanding that a list is either empty [] or it has at least one element and a remainder first_elem : remaining_elems. We can name first_elem and remaining_elems anything we want, and in the code above, we name the first element c and the remaining elements cs. The names aren't important. The important part is the "cons operator" :. ghci> :type (:) (:) :: a -> [a] -> [a]
2 of 3
4
Alternatively, you could think of this problem as a set problem. "Is set A a subset of set B?" This is the same as asking "Is the intersection of set A and set B the same as set A?" Or even "Is the set difference of set A and set B an empty set?" Data.List has some set operations defined that allows you to express these without even converting to sets.
🌐
Unity
discussions.unity.com › questions & answers
how do i check if a list contains elements from another list? - Questions & Answers - Unity Discussions
June 16, 2020 - Hi everyone. I’m doing a app in unity using database and i need to check the information (string) conteined in my database to set some toggles on. How can I do that? I’m stuck on something like this: public List allExercises = new List (); public string[] exercisesSplitString; public void LoadData(){ foreach(string c in exercisesSplitString) { Debug.Log("length: " + exercisesSplitString.Length); Debug.Log("c: " + c); ...
🌐
Reddit
reddit.com › r/learnpython › find all items from one list that are not in another list
r/learnpython on Reddit: Find all items from one list that are not in another list
December 21, 2022 -

Hello all,

I am trying to create a list (list3) of all items that are exist in one list (list1), but do not exist in another list (list2). Any help would be greatly appreciated!

I am using pandas / data frames to create two lists each made of the items in a column in different excel files and I want to identify the items in list1 that are not found in list2.

Basically I have:

Df1 = pd.read_excel(file1)

Df2 = pd.read_excel(file2)

List1 = df1['Column Name']

List2 = df2['Column Name']

List3 = [x for x in List1 if x not in List2]

This is basically just recreating List1 and not omitting the entries that exist in List2.

Thank you all in advance! Please let me know if you need more info

🌐
Stack Overflow
stackoverflow.com › questions › 78705838 › how-to-check-if-elements-of-one-list-are-also-elements-of-another-list-in-r
How to check if elements of one list are also elements of another list in R - Stack Overflow
You could use the lapply function to check whether each element of list1 belongs to list2 iteratively: ... This will return the output as a list. If you want it to return a vector instead, you can use sapply:
🌐
GeeksforGeeks
geeksforgeeks.org › python-check-if-a-list-is-contained-in-another-list
Python | Check if a list is contained in another list - GeeksforGeeks
May 10, 2023 - The task of checking if a list ... in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False....
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-all-elements-of-a-list-are-contained-in-another-list-415148
How to check if all elements of a list are contained in another list | LabEx
Perform Set-like Operations: Combine list membership with other list operations to perform set-like operations, such as finding the intersection, union, or difference between two lists. In Python, the in operator is the primary way to check ...
Top answer
1 of 6
53

When number of occurrences doesn't matter, you can still use the subset functionality, by creating a set on the fly:

>>> list1 = ['a', 'c', 'c']
>>> list2 = ['x', 'b', 'a', 'x', 'c', 'y', 'c']
>>> set(list1).issubset(list2)
True

If you need to check if each element shows up at least as many times in the second list as in the first list, you can make use of the Counter type and define your own subset relation:

>>> from collections import Counter
>>> def counterSubset(list1, list2):
        c1, c2 = Counter(list1), Counter(list2)
        for k, n in c1.items():
            if n > c2[k]:
                return False
        return True
   
>>> counterSubset(list1, list2)
True
>>> counterSubset(list1 + ['a'], list2)
False
>>> counterSubset(list1 + ['z'], list2)
False

If you already have counters (which might be a useful alternative to store your data anyway), you can also just write this as a single line:

>>> all(n <= c2[k] for k, n in c1.items())
True
2 of 6
7

Be aware of the following:

>>>listA = ['a', 'a', 'b','b','b','c']
>>>listB = ['b', 'a','a','b','c','d']
>>>all(item in listB for item in listA)
True

If you read the "all" line as you would in English, This is not wrong but can be misleading, as listA has a third 'b' but listB does not.

This also has the same issue:

def list1InList2(list1, list2):
    for item in list1:
        if item not in list2:
            return False
    return True

Just a note. The following does not work:

>>>tupA = (1,2,3,4,5,6,7,8,9)
>>>tupB = (1,2,3,4,5,6,6,7,8,9)
>>>set(tupA) < set(TupB)
False

If you convert the tuples to lists it still does not work. I don't know why strings work but ints do not.

Works but has same issue of not keeping count of element occurances:

>>>set(tupA).issubset(set(tupB))
True

Using sets is not a comprehensive solution for multi-occurrance element matching.

But here is a one-liner solution/adaption to shantanoo's answer without try/except:

all(True if sequenceA.count(item) <= sequenceB.count(item) else False for item in sequenceA)

A builtin function wrapping a list comprehension using a ternary conditional operator. Python is awesome! Note that the "<=" should not be "==".

With this solution sequence A and B can be type tuple and list and other "sequences" with "count" methods. The elements in both sequences can be most types. I would not use this with dicts as it is now, hence the use "sequence" instead of "iterable".

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-if-the-list-contains-elements-of-another-list
Python Check if the List Contains Elements of another List - GeeksforGeeks
July 23, 2025 - If this condition is true, it prints "Yes" otherwise, it prints "No". ... This method checks each element of one list against the other using a concise and readable generator expression inside the all() function.
🌐
Python Forum
python-forum.io › thread-36106.html
How to check if a list is in another list
January 17, 2022 - Hi All, I have the below code: list_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] win_1 = ['1', '2', '3', '4'] if win_1 in list_1: print('yes')It doesn't print anything, how do I check if a list is in another list?
🌐
OCaml
discuss.ocaml.org › learning
Check if all element in list exists - Learning - OCaml
May 30, 2021 - Hi, so I’m trying to implement a backward chaining in an expert system and I just can’t seem to figure out how to get a bool value which represents “are every elements in the list present in the other list”. Also, is it…
Top answer
1 of 4
4

In C, there are two text related data types: char and strings. A string is a pointer to an array of chars, that by convention is terminated with a NUL character ('\0'). A char is a single byte representing an ASCII character.

You create a string literal by compiling a series of characters contained between opening and closing double quotes: "this is a string literal"

On the other hand, you create a char literal by compiling a single character expression - possibly with a backslash escape - between a pair of single quotes: 't', 'h', 'e', 's', 'e', ' ', 'a', 'r', 'e', ' ', 'c', 'h', 'a', 'r', ' ', 'l', 'i', 't', 'e', 'r', 'a', 'l', 's', '\n'

After the advent of code pages and multi-byte character encodings, C added (grudging) support for "wide chars" (that is, more than 8 bits used to store a single element) and for "multi-byte chars" (that is, character encodings that used 8-bit values, but required more than one to encode a single glyph).

The compiler is seeing your line initializing the array:

char a[]={'a','5ab','na','8s'};

And is assuming that '5ab' and 'na' and '8s' are an attempt, by you, to specify a wide or multi-byte character literal. It knows that characters (not strings) are enclosed in single quotes. It knows that you're using type char. So these can't be strings. But they aren't multi-byte characters, either.

In reality, I think you meant for them to be strings. You're probably coming from python, or bash, or perl or some other language where single quotes can delimit strings. But that's not C.

To create a list of strings, you need to know that strings are of type char * (or const char *). You then want to create an array of them - that's your list:

const char *list_of_str[] = {
    "hello",
    "my name",
    "is George"
};

You'll notice that argv is a list of strings. ;-)

2 of 4
0

You cannot have '5ab' in your a[] array. An array of characters can only hold single characters, such as 'a', '5', or '\n'.

Also, I'm not sure if it's a good idea to to i < (argc) in your for loop. argc is the number of elements that you pass in when your C program is ran. Try i < 4 and make sure the i in your for loop starts at 0.

🌐
TutorialsPoint
tutorialspoint.com › article › python-check-if-a-list-is-contained-in-another-list
Python - Check if a list is contained in another list
July 10, 2020 - 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") 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 · 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.
🌐
Super User
superuser.com › questions › 1746060 › excel-check-if-list-of-items-appear-in-another-list-of-items
EXCEL: check if {list of items} appear in another {list of items} - Super User
October 6, 2022 - I have a list1 and want to check if its listed in List2. This will be used in filter formula in excel as criteria e.g. FILTER($A$1:$E$100,list1=list2). I have tried many other approaches (Vlookup,
🌐
TechBeamers
techbeamers.com › program-python-list-contains-elements
Python Program: Check List Contains Another List Items - TechBeamers
November 30, 2025 - We can use the collections.Counter() class to check if a Python list contains all elements of another list by comparing the two counter objects. If the two counter objects are equal, then the first list contains all elements of the second list, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-test-if-all-elements-are-present-in-list
Test if all elements are present in list-Python - GeeksforGeeks
July 12, 2025 - For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a. Set intersection method is one of the most efficient way to test if all elements of one list are ...