PythiloLesson 3 of 30 · progress kept in this browser

Identity, equality and copies

== asks whether two objects have equal values. is asks whether two names refer to the very same object, the one id(x) identifies. Use is for None and other singletons, and == for everything else. Small ints and short strings are sometimes shared, so is can seem to work on them; never rely on it.

import copy

a = [1, 2]
b = [1, 2]
print(a == b, a is b)  # True False

t = (a, "x")
a.append(3)
print(t)  # the list inside changed

shared = [a, a]
deep = copy.deepcopy(shared)
print(deep[0] is deep[1])  # True
Your task

Write unique_objects(items) dropping repeats of the same object, by identity rather than equality, keeping the first. Write with_item(order, item) returning a new order dict whose "items" list ends with item, leaving order and the list inside it unchanged. Write freeze(data) returning a hashable copy of nested data: lists and tuples become tuples, sets become frozensets, and dicts become tuples of (key, value) pairs sorted by key, all recursively.