+ 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
6 odpowiedzi
+ 7
With my jupyter notebook x performs the same operation as print(x)....
+ 7
HonFu. Many thanks. Learning quite a lot.
+ 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?
+ 4
line 2: [::-1] (inverting by slice) doesn't work with dict, because dicts are unordered.
line 5: values1.values()!
line 6: x??
+ 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).
+ 4
My pleasure. :)