Python Basics: Dictionaries (Quiz)
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 🤗
Excellent, many thanks for clarifying.
Become a Member to join the conversation.
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:
This has a comma after the last key-value pair
However, if we look at these nested dictionaries
None of the entries after the key-value pair for
flowerhave a trailing comma. Why the difference?