Add key_in_dict

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-16 21:30:49 +03:00
parent 3c232e0ec7
commit 69e5e3776b

18
snippets/key_in_dict.md Normal file
View File

@ -0,0 +1,18 @@
---
title: key_in_dict
tags: dictionary,beginner
---
Checks if the given key exists in a dictionary.
- Use the `in` operator to check if `d` contains `key`.
```py
def key_in_dict(d, key):
return (key in d)
```
```py
d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
key_in_dict(d, 'three') # True
```