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

How to assign an item/var as an updated dict

I have a code like this that assigns an item as dict that is an updated different dict:

lang1 = {1:'a',2:'b',3:'c'}
langs = {'lang1':lang1,'lang2':lang1.update({3:'g'})}
print(langs['lang2'])

When I do that I got the output ‘None’. Is there anyway to do this so if I print(langs[lang2][3]) it gets ‘g’

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

>Solution :

dict.update does not return anything, it modifies the existing dict.
if you are using python 3.9 and up you can do

lang1 | {3:'g'}

if you are not then you can do:

{**lang1, 3:'g'}
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