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

Re-defining a dict key value

This code doesn’t work – is there another way I can add a key-value pair with a variable as the value?

BTC_ = {"Name": "BTC", "liq": 57000}
ETH_ = {"Name": "ETH", "liq": 50000}

BTC = BTC_['liq']
ETH = ETH_['liq']

ETHp = ETH / 50000
BTCp = BTC / 50000

BTC_.update['price': BTCp ]
ETH_.update['price': ETHp ]

This code currently gives the error:

Output: builtin_function_or_method object is not subscriptable 

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 :

You call functions with (arguments) after the function name, not [arguments]. And the argument to dict.update() must be a dictionary. So the correct syntax would be:

BTC_.update({'price': BTCp})

But if you’re just setting one element, this is normally done using assignment:

BTC_['price'] = BTCp

.update() is used when you want to set multiple elements by merging with another dictionary.

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