OrderedDict Usage in Python

Level 3114 keystrokes

This creates an OrderedDict that remembers insertion order.

Python code

# OrderedDict is a dict subclass that keeps insertion order
1from collections import OrderedDict
2
# Starts empty
3od = OrderedDict()
# Keys are added in the order a, b, c
4od["a"] = 1
5od["b"] = 2
6od["c"] = 3
# keys() follows insertion order: prints ['a', 'b', 'c']
7print(list(od.keys()))

Output

['a', 'b', 'c']
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
  • (× 4
  • )× 4
  • =× 4
  • [× 3
  • ]× 3
  • .× 1

More Python examples at level 3

← Python typing practice