1
0
mirror of https://github.com/jbranchaud/til synced 2026-03-04 06:58:45 +00:00

Add Iterate Over A Dictionary as a Python TIL

This commit is contained in:
jbranchaud
2026-03-04 00:20:58 -06:00
parent 55a6691681
commit c1f046d196
2 changed files with 36 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
# Iterate Over A Dictionary
Let's say we have a `dict` that contains counts of occurrences for each word in
some sample text:
```python
words_frequency = {
"the": 4,
"a": 3,
"dog": 1,
"bone": 1,
"wants": 1,
...
}
```
Here is how we can iterate over the `dict`, accessing both the keys and values:
```python
for word, count in word_frequency.items():
print(f"- {word} appears {count} time{'' if count == 1 else 's'}")
```
Using the
[`items()`](https://docs.python.org/3/library/stdtypes.html#dict.items) method,
we're able to access both _key_ and _value_ with the for loop as it iterates.
Another approach is to loop directly on the `dict` which implicitly surfaces the
_key_ for iteration. This can then be used to get the value from the `dict`:
```python
for word in word_frequency:
print(f"- {word}: {word_frequency[word]}
```