You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]
326
326
12
327
327
```
328
328
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:
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
+
329
353
## Presence (or not) of an element inside a dictionary
330
354
331
355
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:
0 commit comments