The best answer here is to use all(), which is the builtin for this situation. We combine this with a generator expression to produce the result you want cleanly and efficiently. For example:

>>> items = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
>>> all(flag == 0 for (_, _, flag) in items)
True
>>> items = [[1, 2, 0], [1, 2, 1], [1, 2, 0]]
>>> all(flag == 0 for (_, _, flag) in items)
False

Note that all(flag == 0 for (_, _, flag) in items) is directly equivalent to all(item[2] == 0 for item in items), it's just a little nicer to read in this case.

And, for the filter example, a list comprehension (of course, you could use a generator expression where appropriate):

>>> [x for x in items if x[2] == 0]
[[1, 2, 0], [1, 2, 0]]

If you want to check at least one element is 0, the better option is to use any() which is more readable:

>>> any(flag == 0 for (_, _, flag) in items)
True
Answer from Gareth Latty on Stack Overflow
🌐
Google
developers.google.com › google for education › python › python lists
Python Lists | Python Education | Google for Developers
Python's *for* and *in* constructs are extremely useful, and the first use of them we'll see is with lists. The *for* construct -- for var in list -- is an easy way to look at each element in a list (or other collection).
🌐
W3Schools
w3schools.com › python › ref_func_all.asp
Python all() Function
Remove List Duplicates Reverse ... Python Certificate Python Training ... The all() function returns True if all items in an iterable are true, otherwise it returns False....
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-test-if-all-elements-are-present-in-list
Test if all elements are present in list-Python - GeeksforGeeks
July 12, 2025 - In this method, we convert the first list into a set, which allows for constant time membership checking. By iterating through the second list and checking each element's presence in the hash set, we efficiently test if all elements in the second list exist in the first.
🌐
Tutorialspoint
tutorialspoint.com › python › python_lists.htm
Python - Lists
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example − · list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5]) When the above code is executed, it produces the following result − ... You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.
🌐
GeeksforGeeks
geeksforgeeks.org › python-operation-to-each-element-in-list
Python - Operation to each element in list - GeeksforGeeks
December 19, 2024 - Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
🌐
Programiz
programiz.com › python-programming › list
Python List (With Examples)
December 30, 2025 - We can use a for loop to iterate over the elements of a list. For example, fruits = ['apple', 'banana', 'orange'] # iterate through the list for fruit in fruits: print(fruit) ... Python has many useful list methods that make it really easy to work with lists.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › python-check-if-all-elements-in-a-list-are-same
Python - Check if all elements in a List are same
To check if all elements in a list are the same, we can use all() along with a generator expression to compare each item in the list with the first element.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-list.html
Python Lists
Using the standard indexing scheme, the first element is at index 0, the next at index 1, and so on up to index length-1. Here is the code to create a list of the four strings 'a' 'b' 'c' and 'd'. The list is written within square brackets with the elements separated by commas. ... lst.append(value) - add value to the end of a list, increasing the list's length by 1. Of all the list functions, this one is used the most. ... for var in lst: - loop over a list. On each iteration of the loop, Python points the variable var to the next element
🌐
Programiz
programiz.com › python-programming › methods › built-in › all
Python all()
Become a certified Python programmer. Try Programiz PRO! ... The all() function returns True if all elements in the given iterable are truthy. If not, it returns False. ... Note: Truthy values include non-zero numbers, non-empty sequences, and True. Falsy values include 0, None, False, and ...
🌐
Open Book Project
openbookproject.net › thinkcs › python › english3e › lists.html
11. Lists — How to Think Like a Computer Scientist: Learning with Python 3
Since lists are mutable, we often ... of its elements. The following squares all the numbers in the list xs: Take a moment to think about range(len(xs)) until you understand how it works. In this example we are interested in both the value of an item, (we want to square that value), and its index (so that we can assign the new value to that position). This pattern is common enough that Python provides a ...
🌐
W3Schools
w3schools.com › python › python_lists.asp
Python Lists
There are four collection data types in the Python programming language: List is a collection which is ordered and changeable. Allows duplicate members.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - In Python, you can use the built-in functions all() and any() to check whether all elements or at least one element in an iterable (such as a list or tuple) evaluate to True. Built-in Functions - all( ...
🌐
Codecademy
codecademy.com › learn › dacp-python-fundamentals › modules › dscp-python-lists › cheatsheet
Python Fundamentals: Python Lists Cheatsheet | Codecademy
A slice, or sub-list of Python list elements can be selected from a list using a colon-separated starting and ending point. The syntax pattern is myList[START_NUMBER:END_NUMBER]. The slice will include the START_NUMBER index, and everything until but excluding the END_NUMBER item. When slicing a list, a new list is returned, so if the slice is saved and then altered, the original list remains the same. ... In Python, lists are ordered collections of items that allow for easy use of a set of data.