Python Gem #1: all
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 TrueThis 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.