from collections import Dict
# Initialize a new dictionary with string keys and integer values
var d = Dict[String, Int]()
# Add entries to the dictionary
d["a"] = 1
d["b"] = 2
# Access and remove entries
print(d["a"]) # Access an entry, prints 1
print(d.pop("b")) # Remove and return an entry, prints 2
print(len(d)) # Get the number of entries, prints 1
The Dict
API provides a collection for storing and managing key-value pairs with efficient operations. It is designed to mirror Python's dict
implementation, with performance optimizations for both small and large datasets. It maintains the order of insertion for iteration purposes, ensuring a deterministic order over keys, values, and items.
KeyElement
trait, ensuring they are movable, hashable, and equality comparable. Values must be CollectionElements
.This Dict
API is designed with compatibility in mind for systems or environments that support static typing and require explicit type declarations for collections. Its design and functionalities are inspired by Python's dictionary but adapted to environments with stricter type requirements.