Skip to main content
Log in

Mojo module

dict

Defines Dict, a collection that stores key-value pairs.

Dict provides an efficient, O(1) amortized average-time complexity for insert, lookup, and removal of dictionary elements. Its implementation closely mirrors Python's dict implementation:

  • Performance and size are heavily optimized for small dictionaries, but can scale to large dictionaries.

  • Insertion order is implicitly preserved. Iteration over keys, values, and items have a deterministic order based on insertion.

Key elements must implement the KeyElement trait, which encompasses Movable, Hashable, and EqualityComparable. It also includes CollectionElement and Copyable until we push references through the standard library types.

Value elements must be CollectionElements for a similar reason. Both key and value types must always be Movable so we can resize the dictionary as it grows.

See the Dict docs for more details.

Structs

  • Dict: A container that stores key-value pairs.
  • DictEntry: Store a key-value pair entry inside a dictionary.
  • OwnedKwargsDict: Container used to pass owned variadic keyword arguments to functions.

Traits

  • KeyElement: A trait composition for types which implement all requirements of dictionary keys. Dict keys must minimally be Movable, Hashable, and EqualityComparable for a hash map. Until we have references they must also be copyable.
  • RepresentableKeyElement: A trait composition for types which implement all requirements of dictionary keys and Stringable.

Was this page helpful?