What is wrong with this code, please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

What is wrong with this code, please?

values ={'1':1,'2':2,'3':3,'4':4,'5':5} values1={values[::-1]} newData={'1':10,'3':30} values1.update(newData) x=sum(values1.values1()) x

21st Nov 2019, 8:59 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
6 Answers
+ 7
With my jupyter notebook x performs the same operation as print(x)....
21st Nov 2019, 9:17 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
+ 7
HonFu. Many thanks. Learning quite a lot.
21st Nov 2019, 9:28 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
+ 6
Thanks HonFu. Please, does the inverting work only for lists, or are there any other ones it doesn't work with. What other possibilities exit for inverting this dictionary, for instance?
21st Nov 2019, 9:16 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
+ 4
line 2: [::-1] (inverting by slice) doesn't work with dict, because dicts are unordered. line 5: values1.values()! line 6: x??
21st Nov 2019, 9:12 AM
HonFu
HonFu - avatar
+ 4
You can use indexing for every type of collection that has an order, so for example str, tuple and list. Dicts or sets on the other hand have no order. You can imagine them like buckets with stuff in it. You use dictionaries when the order of items is not important, and you just want to access elements by key. If you run your lines of code one by one in the interpreter, values will be output automatically. But when you write and save a program and want to run it, you'll need print(x).
21st Nov 2019, 9:22 AM
HonFu
HonFu - avatar
+ 4
My pleasure. :)
21st Nov 2019, 9:28 AM
HonFu
HonFu - avatar