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
JavaScript
x
5
1
k=1
2
for i,j in toCart.items():
3
print("(",k,")"," ",i,j)
4
k += 1
5
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:
JavaScript
1
2
1
d[outer_dictionary_or_list][Inner_Dictionary_Or_List][And_So_On]
2
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]