Skip to content

Commit 89f3dbd

Browse files
committed
differences for PR #68
1 parent 3ff5c44 commit 89f3dbd

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎02-dictionaries.md‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,15 @@ d.keys()
293293
```
294294

295295
```output
296-
dict_keys(['alice', 'bob', 'jane', 'tom', 'david'])
296+
dict_keys(['alice', 'bob', 'jane'])
297297
```
298298

299299
```python
300300
d.values()
301301
```
302302

303303
```output
304-
dict_values([12, 18, 24, 54, 87])
304+
dict_values([12, 18, 24])
305305
```
306306

307307
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.
@@ -326,6 +326,20 @@ list(d.values())[0]
326326
12
327327
```
328328

329+
It is also possible to iterate through the keys and items in the dictionary at the same time using the `items` function.
330+
This will return a `key, value` pair, which is very useful when using a dictionary in a `for` loop:
331+
332+
```python
333+
for key, value in d.items():
334+
print("Name:", key, " Age:", value)
335+
```
336+
337+
```output
338+
Name: alice Age: 35
339+
Name: bob Age: 18
340+
Name: jane Age: 24
341+
```
342+
329343
## Presence (or not) of an element inside a dictionary
330344

331345
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:

‎md5sum.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"index.md" "b92970dbb54d63d6543a4c28e4df0a58" "site/built/index.md" "2025-04-15"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-04-15"
77
"episodes/01-introduction.md" "c9d4152827292039d98bda76eac83684" "site/built/01-introduction.md" "2025-04-15"
8-
"episodes/02-dictionaries.md" "7651ec341e2bcf4ebd1f7e2c1882e1b1" "site/built/02-dictionaries.md" "2026-04-16"
8+
"episodes/02-dictionaries.md" "48dacf733a69fc589d2782da74837238" "site/built/02-dictionaries.md" "2026-04-16"
99
"episodes/03-numpy_essential.md" "8d65f302a7edac25aaecb56277b282fc" "site/built/03-numpy_essential.md" "2026-04-13"
1010
"episodes/04-software_package_management.md" "62db2ba2a4291e16d2bdd07b77bfc961" "site/built/04-software_package_management.md" "2025-04-15"
1111
"episodes/05-defensive_programming.md" "6d48a7e584a970aacdad58a28b0b9570" "site/built/05-defensive_programming.md" "2026-04-13"

0 commit comments

Comments
 (0)