+ 1
In python dictionary, if a key has two or more value ,is it possible to print one of the value from that particular key? I
Nico = { "name":."Rex', "hobbies":["cycling","coding","culling"],"nationality":"Australia" } how do I print out "coding" as the main hobby without using a sentence..
5 Respostas
+ 2
Well if you access your dictionary with the key "hobbies" you will get the following result :
["cycling","coding","culling"]
This is an array (list) of elements
All elements of an array are indexed based on their order, beginning at 0
So, in your array, 0 -> cycling, 1-> coding and 2 -> culling
If you want to get the second element of your array (coding), you need to access your array this way :
name_of_the_array[1]
Don't forget that the second element correspond to the indice 1, since it starts from 0
So, if you want to do it in one line, that gives :
value = Nico["hobbies"][1]
+ 1
Well when you access the dictionary with your key, it will give you the content associated to the key. In this case, you'll get the array of values. All you need to do is simply get the one you want from that array using an indice
+ 1
How?
0
Oooh! I've seen my error, thanks very much Swift
0
You're welcome !