3 ways to check for multiple keys in a Python dictionary

preview_player
Показать описание
In this video we show you 3 ways to check for multiple keys in a dictionary:
- Use in multiple times
- Use the all() built-in function
- Use set operations, specifically the issubset() method on a set that can take a dictionary as is.

Рекомендации по теме
Комментарии
Автор

Here is another way, using operator.itemgetter. itemgetter takes one or more keys for arguments, and returns a function that take a dict and returns the tuple of values corresponding to those keys. If any key is not present, itemgetter raises KeyError.

```
import operator
try:
values = operator.itemgetter('bob', 'julian', 'tim')(d)
except KeyError:
print("not all in d")
else:
print(f"all in d, with corresponding values {values!r}")
```

ptmcg
welcome to shbcf.ru