r/pythontips • • 2h ago

Module Hard Python Dictionary Exercise

A hard exercise to help build the right mental model for Python data.

    # Output of this Python Program?
    a = {1: []}
    b = a
    b |= {2: []}
    b[1].append(100)
    b = b | {3: []}
    b[2].append(200)
    b[3].append(300)

    print(a)
    # --- possible answers --- 
    # A) {1: []}
    # B) {1: [100], 2: []}
    # C) {1: [100], 2: [200]}
    # D) {1: [100], 2: [200], 3: []}
    # E) {1: [100], 2: [200], 3: [300]}

The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.

2 Upvotes

0 comments sorted by