[Fixed]-The behavior of dictionary and list

1👍

The key problem is where you’ve defined the b dictionary. You’ve done it before the loop, but needed to do it inside:

for p in post:
    b = {}
    b[p.pub_date.year] = p.pub_date.month
    a.append(b)

Or, with a list comprehension:

 a = [{p.pub_date.year: p.pub_date.month} for p in post]
👤alecxe

Leave a comment