+ 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..

18th May 2019, 12:43 PM
Femi SONI Y
Femi SONI Y - avatar
5 Respuestas
+ 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]
18th May 2019, 1:01 PM
ThewyShift
ThewyShift - avatar
+ 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
18th May 2019, 12:48 PM
ThewyShift
ThewyShift - avatar
+ 1
How?
18th May 2019, 12:50 PM
Femi SONI Y
Femi SONI Y - avatar
0
Oooh! I've seen my error, thanks very much Swift
18th May 2019, 1:15 PM
Femi SONI Y
Femi SONI Y - avatar
0
You're welcome !
18th May 2019, 1:17 PM
ThewyShift
ThewyShift - avatar