Python Dictionary Object: SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Viewed 29875

I am creating a dictionary object, it gets created while I use "Statement 1", however I get an error message while try create a dictionary object using same keys and values with "Statement 2".

Statement 1:

dmap = {0: 'Mon', 1: 'Tue', 2: 'Wed', 3: 'Thu', 
        4: 'Fri', 5: 'Sat', 6: 'Sun'}

Statement 2:

dmap = dict(0='Mon', 1='Tue', 2='Wed', 3='Thu', 
            4='Fri', 5='Sat', 6='Sun'

Error message:

    File "<stdin>", line 1
    SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Can someone tell me, why am I allowed to create dictionary with integer keys using Statement 1, but not with Statement 2?

Edited Using an updated version of Statement 2, I am able to create dictionary object with below code:

dmap = dict(day_0='Mon', day_1='Tue', day_2='Wed', 
            day_3='Thu', day_4='Fri', day_5='Sat', day_6='Sun')
8 Answers

I ran into the problem because of a missing comma in my dictionary:

item = {
    'a': 1,
    'b': 2
    'c': 3,
    'd': 4
}   

Note the missing comma between 2 and 'c'

Your confusion probably comes from the fact that you can write things like:

d = dict(spam=1, eggs=2)

This uses a feature called "keyword arguments" and it only works if the keys are strings. Therefore, your example doesn't work because the keys are integers.

dict is a regular callable which accepts keyword arguments. As per the Python syntax, keyword arguments are of the form identifier '=' expression. An identifier may not start with a digit, which excludes number literals.

keyword_item ::= identifier "=" expression

That dict does by default create a dictionary that accepts arbitrary keys does not change the syntax of calls.

Because in statement 2 you use "=" instead of ":". you have to use ":".

Right way:

dict_obj = {'a': 1, "b": 2, "c": 3}

Wrong way:

dict_obj = {'a' = 1, "b" = 2, "c" = 3}

This is just how dictionary works.

Whereas 0:'Mon' depicts a key-value pair and gets evaluated as such, 0='Mon' gets evaluated as if you would write 0 = 'Mon' in regular code, which would try to assign 'Mon' to a number, not a variable, and would be like writing 'Potato' = 'Mon' - kind of nonsensical.

you can use integers as key in dict function but not like that you can do that like this:

dmap = dict([(0 ,'Mon') ,(1, 'Tue'), (2, 'Wed'), (3, 'Thu'),(4, 'Fri'),(5 , 'Sat'), (6, 'Sun')])

A dictionary in python must contain a key-value pair. like

dmap = {0 : 'Mon', 1 : 'Tue', 2 : 'Wed', 3 : 'Thu', 4 : 'Fri', 5 : 'Sat', 6 : 'Sun'}

here keys are 0, 1, 2, 3, 4, 5, 6 and their respective values are 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
Note: The keys or the values can be of any type.

But in statement 2: dmap = dict(0 = 'Mon', 1 = 'Tue', 2 = 'Wed', 3 = 'Thu', 4 = 'Fri', 5 = 'Sat', 6 = 'Sun') You are assigning values. This is not allowed in python.

Instead you can write like this.

dmap = dict({0 : 'Mon', 1 : 'Tue', 2 : 'Wed', 3 : 'Thu', 4 : 'Fri', 5 : 'Sat', 6 : 'Sun'})
Related