Class collections.defaultdict

Create an int variable that holds 0

#Two ways to do the same thing:

i = 0
i = int()   #no arguments

A dictionary whose values are ints

Without the yellow code, the first execution of the statement d[person] += 1 would raise the KeyError exception because d does not have the key "Derek".

"""
Count how many times each person appears.
"""

import sys

people = ("Derek", "Dave", "Derek", "Don", "Dean", "Dan", "Derek", "Dan", "Dave")

d = {}   #Create an empty dict

for person in people:
    if person not in d:
        d[person] = int() #or d[person] = 0
    d[person] += 1        #means d[person] = d[person] + 1

for person in sorted(d):   #alphabetical order
    print(f"{person:5} {d[person]}")

sys.exit(0)
Dan   2
Dave  2
Dean  1
Derek 3
Don   1

A collections.defaultdict whose values are ints

During the first iteration of the first for loop, the variable person contains "Derek" and we add 1 to the 0 in d["Derek"]. Where did this 0 come from? It was created by the int function passed as an argument to the collections.defaultdict function.

"""
Count how many times each person appears.
"""

import sys
import collections

people = ("Derek", "Dave", "Derek", "Don", "Dean", "Dan", "Derek", "Dan", "Dave")

d = collections.defaultdict(int)   #Create an empty collections.defaultdict

for person in people:
    d[person] += 1

for person in sorted(d):   #alphabetical order
    print(f"{person:5} {d[person]}")

sys.exit(0)
Dan   2
Dave  2
Dean  1
Derek 3
Don   1

Later, we’ll have an even easier way to do this with a collections.Counter.

Create an empty list.

#Two ways to do the same thing:

list1 = []
list2 = list()

A dictionary whose values are lists

In the dictionary d, each key is a string naming a day of the week, and each value is a list of people.

"""
List the appointments for each day of the week.
"""

import sys

appointments = (     #a tuple of 8 tuples
    ("Monday",    "John"),
    ("Wednesday", "Dan"),
    ("Sunday",    "Jane"),
    ("Tuesday",   "John"),
    ("Thursday",  "Mary"),
    ("Wednesday", "Bob"),
    ("Wednesday", "Bill"),
    ("Sunday",    "Jim")
)

d = {}   #Create an empty dict

for appointment in appointments:
    day = appointment[0]
    name = appointment[1]
    if day not in d:
        d[day] = list()   #or d[day] = []
    d[day].append(name)

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

for day in sorted(d, key = lambda day: days.index(day)):
    names = sorted(d[day])
    print(f'{day + ":":10} {", ".join(names)}')

sys.exit(0)
Sunday:    Jane, Jim
Monday:    John
Tuesday:   John
Wednesday: Bill, Bob, Dan
Thursday:  Mary

A collections.defaultdict whose values are lists

In the collections.defaultdict d, each key is a string naming a day of the week, and each value is a list of people.

During the first iteration of the first for loop, the variable day contains "Monday" and we append the string "John" to the empty list d["Monday"]. Where did this empty list come from? It was created by the list function passed as an argument to the collections.defaultdict function.

"""
List the appointments for each day of the week.
"""

import sys
import collections

appointments = (     #a tuple of 8 tuples
    ("Monday",    "John"),
    ("Wednesday", "Dan"),
    ("Sunday",    "Jane"),
    ("Tuesday",   "John"),
    ("Thursday",  "Mary"),
    ("Wednesday", "Bob"),
    ("Wednesday", "Bill"),
    ("Sunday",    "Jim")
)

d = collections.defaultdict(list)   #Create an empty collections.defaultdict

for appointment in appointments:
    day = appointment[0]
    name = appointment[1]
    d[day].append(name)   #d[day] is a list

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

for day in sorted(d, key = lambda day: days.index(day)):
    names = sorted(d[day])
    print(f'{day + ":":10} {", ".join(names)}')

sys.exit(0)
Sunday:    Jane, Jim
Monday:    John
Tuesday:   John
Wednesday: Bill, Bob, Dan
Thursday:  Mary