Interview Prep/Python for DevOps/Language essentials & functions

How do `any()` and `all()` short-circuit, and where do you use them?

BeginnerConceptLanguage essentials & functions
Answer

any(iterable) returns True at the first truthy item; all(iterable) returns False at the first falsy item. Both stop consuming the iterable early, so with a generator they avoid unnecessary work.

open_to_world = any(r.get("CidrIp") == "0.0.0.0/0" for r in perm["IpRanges"])
all_ready = all(c.status == "True" for c in node.status.conditions if c.type == "Ready")

Note all([]) is True, which can make an empty result look like success.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions