Python Basics: Dictionaries (Quiz)

tonypy on Feb. 20, 2023

Can you clarify why some of the example dictionaries include a trailing comma after the last entry and others don’t? As an example:

my_dog = {
    "name": "Frieda",
    "age": 6,
    "nicknames": ["Fru-Fru", "Lady McNugget"],
    "hungry": True,

This has a comma after the last key-value pair "hungry": True

However, if we look at these nested dictionaries

states = {
    "California": {
        "capital": "Sacramento",
        "flower": "California Poppy"
    "New York": {
        "capital": "Albany",
        "flower": "Rose"
    "Texas": {
        "capital": "Austin",
        "flower": "Bluebonnet"

None of the entries after the key-value pair for flower have a trailing comma. Why the difference?

Philipp Acsany RP Team on Feb. 21, 2023

Thanks for pointing the trailing comma out, @9relpyn53! 🙂

In Python dictionaries, you can do both:

  • Omit the comma on the last item
  • Add a comma to the last item

Both are valid options. I tend to use the trailing comma, because then I don’t need to remember to add the comma when I manually add a new item.

That being said, I should have mentioned this in the course. Sorry for the confusion 🤗

tonypy on Feb. 21, 2023

Excellent, many thanks for clarifying.

Become a Member to join the conversation.