Update index.md with has_key() (#21093)

Adding has_key() method to python dictionaries
This commit is contained in:
Adi
2018-11-07 10:32:45 -08:00
committed by nik
parent 9e781f4e3e
commit 5ee74650f1

View File

@ -65,6 +65,15 @@ It's easy to add key-value pairs to an existing dictionary:
>>> club_names
[1910, 1875]
```
**`has_key`** is a built-in *method* that can be used to check if a *key* exists in the dictionary.
```python
>>> items = {'a': 'apple', 'b': 'banana', 'c': 'cat'}
>>> items.has_key('a')
True
>>> items.has_key('d')
False
```
Yet another way of creating dictionary is using the **`dict()`** method:
```python
>>> players = dict( [('messi','argentina'), ('ronaldo','portugal'), ('kaka','brazil')] ) # sequence of key-value pair is passed