Nested Dict in Python

Level 3111 keystrokes

This accesses a value in a nested dictionary using chained bracket notation.

Python code

# Create a dictionary where values are also dictionaries
1users = {
# alice's data is a nested dictionary
2 "alice": {"age": 30, "city": "NYC"},
# bob's data is also a nested dictionary
3 "bob": {"age": 25, "city": "LA"}
4}
# First bracket gets alice's dict, second gets city value
5print(users["alice"]["city"])

Output

NYC
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.

  • "× 20
  • :× 6
  • ,× 3
  • {× 3
  • }× 3
  • [× 2
  • ]× 2
  • (× 1
  • )× 1
  • =× 1

More Python examples at level 3

← Python typing practice