Example 1 — Start an Attendance Sheet with False for Everyone
Mark every student absent at first, then record one arrival.
students = ["Maya", "Leo", "Iris"]
attendance = dict.fromkeys(students, False)
print(attendance)
attendance["Leo"] = True
print(attendance)
print(sum(attendance.values()), "present")
print(dict.fromkeys("abc")) How It Works
- One value for all. Each name from the list becomes a key, and every key starts as
False. - Replacing is safe. Assigning
Trueto Leo puts a new object under one key; the other keys keepFalse. - Booleans add up.
Truecounts as 1 insum(), so the sheet reports 1 present. - String keys, default value. A string is iterated character by character, and leaving out the value gives every key
None.

