Back to Module 4

Dictionaries

A dictionary stores values indexed by unique keys.

freq = {
    "the": 69033,
    "of": 36998,
    "and": 30157
}

print(freq["the"])
freq["to"] = 21892
freq["and"] = 30500
del freq["of"]

Common methods

  • dict.keys()
  • dict.values()
  • dict.items()
  • dict.get(key, default)
for word in freq.keys():
    print(word)

for count in freq.values():
    print(count)

for word, count in freq.items():
    print(f"{word}: {count}")

print(freq.get("of", 0))

Next: More about functions