0
list2 = [“apples”,”pear”,”cookie”]
list1 = [“apples”,”pear”,”cookie”, “popcorn”, “candy”]
Check = all(item in list1 for item in list2)

If Check:
   print(“items in list2 is also in list1”)

Im confused on how exactly the all(item in list1 for item in list2) works and i was wondering if someone could explain the process taking place

Ice
  • 3
  • 1

1 Answers1

0

The equivalent form of all in Python:

def all(iterable):
    for val in iterable:
        if not val:
            return False
    return True

Because you are passing a generator, it will yield the value of item in list1 for each item in list2 one by one, so it is iterable.

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31