Reputation: 21
first i am new to python and doing an assignment for class i am writing a program where i make a dictionary with the following information child andrew,betsy,louise,chad mother jane,eleen,natalie,mary father john,nigel,louis,joseph. It says to use a list for the value so i wrote it
dic={ "child": ["andrew", "betsy", "louise", "chad"],
"mother": ["jane", "ellen", "natalie", "mary"],
"father": ["john", "nigel", "louis", "joseph"]}
now the idea is that the program will give you options if you would like to know the mother,father, or both. then you enter the childs name and it gives you the corresponding mother,father, or both. but i am just trouble shooting at the moment and want to know how could i call a element of each list with it in a dictionary like this for instance say i wanted to make it print andrew if i put print
print dic["child"]
it will print that whole list but how can i make it just print the one in the [0] position i tried
print dic["child"[0]]
and
print dic["child"(0)]
both give an error.
Upvotes: 1
Views: 54
Reputation: 11635
You're indexing is incorrect you should be indexing like so print dict['child'][0]
Doing dict['child'[0]])
is indexing the string "child" so your passing the key c
to the dictionary which doesn't actually have this key inside the dict.
dict['child']
is grabbing the value of the dictionaries key "child". So, when you print this you'll see the entire list of values, and when you do
dict['child'][0]
you're indexing this list of values so you'll see the correct value "andrew"
Upvotes: 1