Computation for Linguists

Nested Dicts

Dr. Andrew M. Byrd

2025-10-31

Review

  • What did you learn last time?

Recap from Last Time

  • Lists ["a", "b", "c"]
  • Dictionaries {key: value}
  • pd.DataFrame

Recap from Last Time

list = ["eeny", "meeny", "miney", "mo"]

for i in list:
  for c in i:
    print(c)

Recap from Last Time

sentences = [
    ["the", "cat", "sleeps"],
    ["the", "dog", "runs"],
    ["the", "bird", "flies"]
]

for sentence in sentences:
  for w in sentence:
    print(w)

Recap from Last Time

list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3]
list3 = ["alpha", "beta", "gamma", "delta"]
list4 = ["fee", "fie", "fo", "fum"]


list2dict = {i: [j, k, l] for i, j, k, l in zip(list1, list2, list3, list4)}

list2dict

Review Activity

  • Copy the below lists.
  • Add a fourth list called length, whose items contain the length of each word in words.
  • Then, convert to a dictionary using the zip() function, with the keys = words.
words = ["zombie", "ghoul", "ghost", "vampire"]
begins_with_g = [False, True, True, False]
rhymes_with_most = [False, False, True, False]

Nested Dictionaries

Nested Dictionaries

  • A nested dict is just a dictionary inside of another dictionary
outer = {
    "key1": {"inner_key1": "value1", "inner_key2": "value2"},
    "key2": {"inner_key1": "value3", "inner_key2": "value4"}
}

When to Use Nested Dictionaries

  • You should use nested dictionaries only when they make sense – whenever one piece of data naturally contains multiple sub-parts
students = {
    "Alice": {"quiz": 9, "homework": 10, "final": 8},
    "Ben": {"quiz": 7, "homework": 9, "final": 10}
}

When to Use Nested Dictionaries

  • What could this look like within a linguistic data set?
consonants = {
    "p": {"voice": False, "place": "bilabial", "manner": "stop"},
    "t": {"voice": False, "place": "alveolar", "manner": "stop"},
    "k": {"voice": False, "place": "velar", "manner": "stop"}
}

Accessing Nested Items

list = ["eeny", "meeny", "miney", "mo"]
list[0][0]

Accessing Nested Items

sentences = [
    ["the", "cat", "sleeps"],
    ["the", "dog", "runs"],
    ["the", "bird", "flies"]
]
sentences[0][0]
  • What would sentences[0][0][0] give us?

Accessing Nested Items

students = {
    "Alice": {"quiz": 9, "homework": 10, "final": 8},
    "Ben": {"quiz": 7, "homework": 9, "final": 10}
}

students["Alice"]["quiz"]

Converting to pd.DataFrame

  • What happens when we convert a nested dict to a pd.DataFrame?
import pandas as pd
consonants = {
    "p": {"voice": False, "place": "bilabial", "manner": "stop"},
    "t": {"voice": False, "place": "alveolar", "manner": "stop"},
    "k": {"voice": False, "place": "velar", "manner": "stop"}
}
df = pd.DataFrame(consonants)
df

Converting Nested dicts to pd.DataFrame

  • By default, when you pass a nested dict like that to pd.DataFrame
  • Pandas interprets the outer keys (“p”, “t”, “k”) as columns, and the inner keys (“voice”, “place”, “manner”) as row labels (the index)

Converting Nested dicts to pd.DataFrame

  • So we need to flip the table around, making the phonemes the index (or a column unto themselves)

    • This is what the .T method does – it swaps the rows and columns of a table
import pandas as pd
consonants = {
    "p": {"voice": False, "place": "bilabial", "manner": "stop"},
    "t": {"voice": False, "place": "alveolar", "manner": "stop"},
    "k": {"voice": False, "place": "velar", "manner": "stop"}
}

df = pd.DataFrame(consonants).T.reset_index(names="phoneme")
df

dict Activity

  • Copy the lists on the next slide. Using a for loop and the zip() function, create a dictionary with the phonemes as keys.
new_dict = {}
for i, j, k, l in zip(list1,list2,list3,list4):
  new_dict[i] = {
    "key1": j,
    "key2": k,
    "key3": l
}
  • Then, using the .T method, convert it into a pd.DataFrame.

dict Activity

phonemes = ["p", "b", "t", "d", "k", "g"]
voicing  = [False, True, False, True, False, True]
place    = ["bilabial", "bilabial", "alveolar", "alveolar", "velar", "velar"]
manner   = ["stop"] * 6