Why does my dictionary have a syntax error?


#1

Question

Why does my dictionary have a syntax error?

Answer

Be sure you’re following the syntax of a dictionary exactly, otherwise you’ll get a syntax error. A dictionary requires:
Using curly braces { }
Key name on the left, colon : in the middle, and a value on the right
Be sure to put a comma , after every value except the last
For example:

my_dict = {
  "key1" : 1,  # key name : value, 
  "key2" : 2,  # NOTE THE COMMA AT THE END
  "key3" : 3
}

FAQ: Learn Python - Advanced Topics in Python - Iterators for Dictionaries