Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions episodes/02-dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ d.keys()
```

```output
dict_keys(['alice', 'bob', 'jane', 'tom', 'david'])
dict_keys(['alice', 'bob', 'jane'])
```

```python
d.values()
```

```output
dict_values([12, 18, 24, 54, 87])
dict_values([12, 18, 24])
```

Note that the *dict\_keys* and *dict\_values* objects are iterable but are not lists. This means that they can be used somewhere like a `for` loop but you can not index them directly.
Expand All @@ -326,6 +326,30 @@ list(d.values())[0]
12
```

It is also possible to iterate through the keys and items in the dictionary at the same time using the `items` function,
which returns a *dict\_items* object containing `key, value` pairs:

```python
d.items()
```

```output
dict_items([('alice', 35), ('bob', 18), ('jane', 24)])
```

This is very useful when using a dictionary in a `for` loop:

```python
for key, value in d.items():
print("Name:", key, " Age:", value)
```

```output
Name: alice Age: 35
Name: bob Age: 18
Name: jane Age: 24
```

## Presence (or not) of an element inside a dictionary

It is possible to test if a key is present in the dictionary (or not) using the keyword `in`, just as we did at the start of this lesson for values within a list:
Expand Down
Loading