← All lessons

any, all, zip, enumerate

Sign in · Sign up

Lesson 11.8

Four built-in functions that make code shorter:

Function What it does
any(...) at least one True
all(...) all are True
zip(a, b) pairs from two lists
enumerate(a) pairs of "number, value"
print(any(s == 5 for s in scores))
print(all(s >= 3 for s in scores))

for name, age in zip(names, ages):
    print(name, age)

any([4, 5]) is a mistake in meaning: numbers are not True/False, comparisons are needed. enumerate has start=1 when numbering should begin at one.

The check sets the list of numbers scores, the names names and the ages ages. Print:

  1. whether any item of scores is target;
  2. whether all the numbers are at least lowest;
  3. every pair name age from zip;
  4. every pair number name from enumerate, numbered from 1.
Hint

any and all take a generator of conditions.

any needs the comparison s == target, all needs s >= lowest. zip gives pairs, enumerate a "number and item" pair; numbering from one comes from start=1.