Dictionaries
A list finds items by position. A dictionary finds them by a key you choose, such as a name. Each
entry pairs a key with a value, written key: value:
const ages = ["Ada": 36, "Grace": 85]print(ages)print(ages.count)["Ada": 36, "Grace": 85]2Like a list, a dictionary holds one type of key and one type of value. ages is a
Dict[String, Int]: keys that are strings, values that are whole numbers.
Looking up a value
Section titled “Looking up a value”Square brackets look a value up by its key:
const ages = ["Ada": 36, "Grace": 85]print(ages["Ada"])print(ages["Bob"])36nothingA key that isn’t there gives nothing, so a lookup is optional: its type is Int?.
That means you have to decide what a missing key means before using the value:
const ages = ["Ada": 36]print(ages["Ada"] + 1)dictionaries.em:2:7: addition needs numbers, but this is Int? and Int print(ages["Ada"] + 1) ^^^^^^^^^^^^^^^One of these may be absent. Give it a fallback with `.or(0)`, or check it against `nothing` first.Often a fallback is exactly right:
const ages = ["Ada": 36]print(ages["Ada"].or(0) + 1)print(ages["Bob"].or(0))370contains_key?("Bob") asks whether a key is there without looking up its value.
Adding and changing entries
Section titled “Adding and changing entries”A dictionary declared with var can change. Assigning to a key adds the entry, or replaces its value
if the key is already there; remove takes one out:
var ages = ["Ada": 36]ages["Grace"] = 85 # addages["Ada"] = 37 # replaceprint(ages)ages.remove("Ada")print(ages)["Ada": 37, "Grace": 85]["Grace": 85]A key appears at most once, so assigning to an existing key never makes a second entry.
An empty dictionary
Section titled “An empty dictionary”An empty dictionary is written [], with a type saying what it will hold. It prints as [:], so
you can tell it from an empty list:
var stock: Dict[String, Int] = []print(stock)stock["apples"] = 3print(stock)[:]["apples": 3]Going through every entry
Section titled “Going through every entry”A for loop visits each entry, giving the key and the value names of their own:
const ages = ["Ada": 36, "Grace": 85]for (name, age) in ages { print("#{name} is #{age}")}Ada is 36Grace is 85Entries come out in the order they were added. keys() and values() give lists of just the keys
or just the values.
A dictionary that counts
Section titled “A dictionary that counts”Dictionaries are the natural way to count things. Here each word is a key, and its value is how many times it has appeared so far:
const words = "the cat and the hat".split(" ")var counts: Dict[String, Int] = []for word in words { counts[word] = counts[word].or(0) + 1}print(counts)["the": 2, "cat": 1, "and": 1, "hat": 1]counts[word].or(0) is the count so far, or 0 the first time a word is seen.
List or dictionary?
Section titled “List or dictionary?”- Use a list when order matters and you think of items by position: a queue, a leaderboard, the lines of a file.
- Use a dictionary when you look things up by a name or other key: a phone book, stock levels, settings.
The Dict reference describes every method.
What you’ve learned
Section titled “What you’ve learned”- A dictionary,
["Ada": 36], maps keys to values, one type of each. dict[key]looks a value up and might benothing;.or(fallback)handles a missing key.dict[key] = valueadds or replaces an entry, andremove(key)takes one out.- An empty dictionary is
[]with a type, and prints as[:]. for (key, value) in dictvisits the entries in the order they were added.
Next, sets: collections where each value appears once.