Skip to content
Advertisement

How to access dictionary values

I am trying to print dictionaries keys and values separately, the dictionary that’ve created consist of 2 values in a list: {‘item’:[val1,val2]}

How can i access the values separately?

This is my code where i tried to access the value of val1, but am getting both of val1 & val2

k=1
for i,j in toCart.items():
                print("(",k,")"," ",i,j)
                k += 1

Advertisement

Answer

This was answered here: How to access nested dictionary in python

Only instead of the second key you put the index

This kind of things are called nested dictionaries/lists To call them, here is the syntax:

d[outer_dictionary_or_list][Inner_Dictionary_Or_List][And_So_On]

To get a list of dictionary keys, go here: How to return dictionary keys as a list in Python?

To get a value from a key, do dictionary_name[key_name]

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement