Skip to content

Commit afada0f

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : a5aaab9 Branch : main Author : scottan <33283688+Scottan@users.noreply.github.com> Time : 2026-04-16 13:57:37 +0000 Message : Merge pull request #68 from UoMResearchIT/53-add-items-method-to-spintting-out-keys-and-values feat: example added for .items() function
1 parent 4126380 commit afada0f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

02-dictionaries.md

Lines changed: 26 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,30 @@ 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+
which returns a *dict\_items* object containing `key, value` pairs:
331+
332+
```python
333+
d.items()
334+
```
335+
336+
```output
337+
dict_items([('alice', 35), ('bob', 18), ('jane', 24)])
338+
```
339+
340+
This is very useful when using a dictionary in a `for` loop:
341+
342+
```python
343+
for key, value in d.items():
344+
print("Name:", key, " Age:", value)
345+
```
346+
347+
```output
348+
Name: alice Age: 35
349+
Name: bob Age: 18
350+
Name: jane Age: 24
351+
```
352+
329353
## Presence (or not) of an element inside a dictionary
330354

331355
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" "83b33b6c48be79687521d24eae56c974" "site/built/02-dictionaries.md" "2026-04-16"
99
"episodes/03-numpy_essential.md" "72a9aa41a65900b4a87744e8f946fa00" "site/built/03-numpy_essential.md" "2026-04-16"
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)