Dict Fromkeys in Python

Level 258 keystrokes

This creates a dictionary from a list of keys with a default value.

Python code

# Create a list of the keys to use
1keys = ["a", "b", "c"]
# dict.fromkeys(keys, 0) builds a new dictionary with every key mapped to 0
# All keys share the same value object, so avoid a mutable default like []
2d = dict.fromkeys(keys, 0)
# Prints {'a': 0, 'b': 0, 'c': 0}
3print(d)

Output

{'a': 0, 'b': 0, 'c': 0}
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 6
  • ,× 3
  • (× 2
  • )× 2
  • =× 2
  • .× 1
  • [× 1
  • ]× 1

More Python examples at level 2

← Python typing practice