What if there is an empty space in the dictionary key value and the value is updated in that key?

Hi,
First of all, please understand that I wrote it using a translator.
I’m using Python version 3.10.1

What if there is an empty space in the dictionary key value and the value is updated in that key?

For example

dict = {‘hands cream’:1000}

#test1
dict.update(hands cream=500} => error

#test2 - error
dict.update(‘hands cream’=500} => error

#test3
dict.update(handscream=500} => add dict {‘hands cream’: 1000, ‘handcream’: 500}

How can I update it?

Please guide me and let me know if I am missing anything. Thanks in advance and I look forward to hearing from you.

Best Regards,
Jacob

And you don’t have built-in dict anymore, instead you have instance:

+>>> dict
<class 'dict'>
+>>> dict = dict()
+>>> dict
{}

Don’t use dict as the name of a dictionary. It can cause issues in the future.

You’re attempting to use the “keyword assignment” form, but that doesn’t work with keys that aren’t compatible with variables (like containing spaces). So you’ll have to use one of the other forms.

d = {'hands cream': 1000}
d['hands cream'] = 500 # direct modification
d.update({'hands cream': 500}) # update via dictionary
d.update([('hands cream', 500)]) # update via iterable (list here) of 2-element sequences (tuple here)
1 Like

As Aivar and BowlOfRed has already said, don’t use the name of built-in

dict as the name of a variable.

# Better

mydict = {'hands cream': 1000}



# Update the value

mydict.update({'hands cream': 999})



# Or like this.

mydict['hands cream'] = 555

Thank you for everyone’s answers.
I understood clearly.
Once again, thank you for everyone’s help with everyone’s busy time.