Python Gem #1: all

Adam Short
Aug 27, 2017 · 1 min read

Python has this little built in function called all.

all(iterable) -> returns True if all elements in iterable are True

This comes in really handy with checking if an operation is true on all elements of a list:

if all(p.isBot for p in players):
... do things ...

Super Trick

Need to check if a dictionary contains all of a set of keys?

if all(key in case for key in [k1, k2, k3] for case in cases):
... do things ...

Simple!


This is a daily series called Python Gems. Each short posts covers a detail or a feature of the python language that you can use to increase your codes readability while decreasing its length.

)

Adam Short

Written by

full stack engineer 🐺 18. works @ Stella AI

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade