Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 452
Deepak.K Avatar asked Jun 08 '26 07:06

Deepak.K


1 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'

like image 128
James Shapiro Avatar answered Jun 10 '26 22:06

James Shapiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!