+ 2

In a dictionary can we access the elements of this ..... Here it is-------- dic= {1:10,2:20,3:30,4:40}

28th Aug 2016, 6:56 PM
LALIT NEGI
LALIT NEGI - avatar
3 Answers
+ 3
You can access to the elements using the key. For example, if you want to check the values for each key i would do It so: print dic[1] print dic[2] etc... You can even change the values of the elements assigned to the Keys. Ex: dic[1]=5 (You have changed the value associated to the key 1 to 5 when at the beggining it was 10. But you can never change the Keys. This is a good example: https://youtu.be/DSzqe4Rb5YY
28th Aug 2016, 9:57 PM
Carlos Sacanell
Carlos Sacanell - avatar
0
Doesn't dic[1] refer to the second key in the dictionary? In your example dic[1]=5 would mean that the dictionary is changed to: -- dic={1:10,2:5,3:30,4:40} Or am I wrong?
31st Aug 2016, 11:00 AM
Rudy OV
Rudy OV - avatar
0
This is not a list. As he gave to the key 1 the value 10 dic[1]=10 When you give 5 as value of the key 1 dic[1] = 5 It will be: dic = {1:5,...} because dictonaries are not indicized, even if in this example ot starts from 1. The key can be a string ...
31st Aug 2016, 5:16 PM
Giovanni Gatto
Giovanni Gatto - avatar