Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Different ways to create dictionary in python

I was exploring different ways to create a dictionary in Python 3 (3.11.1). ChatGPT suggested that I can create a dictionary by using the following syntax:

my_dict = dict('key1'='value1', 'key2'='value2', 'key3'='value3')

Suggestion from ChatGPT

I tried this in IDLE, but I am getting SyntaxError: expression cannot contain assignment, perhaps you meant "=="?.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error thrown by IDLE

To confirm if ChatGPT may have suggested the wrong method, I asked the same question with Google Bard. Ever Bard is replying that this method can be used to create a dictionary. Am I missing something?

Google Bard's reply

>Solution :

Hey don’t give the key1 and key2 and key3 quotes. key names should not be in quotes then it will work

my_dict = dict(key1='value1', key2='value2', key3='value3')
print (my_dict)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading