0

I have a python dictionary which is described below:

dict={ "Moli": {"Buy": 75, "Sell": 53, "Quantity": 300} "Anna": "Buy": 55, "Sell": 83, "Quantity": 154} "Bob": {"Buy": 25, "Sell": 33, "Quantity": 100} "Annie": {"Buy": 74, "Sell": 83, "Quantity": 96} } 

I want to select or print first item's sub-value (i.e: "Buy": 75) of this nested dictionary.

If I am using this code:

print(trading_portfolio[(list(trading_portfolio.keys())[0])]["Buy"]) 

I'm getting error like this:

select first item from this nested dictionary builtins.KeyError: "Buy" 
1
  • 1
    Welcome to SO! Your code works fine for me but next(iter(d.values()))["Buy"] seems preferable. Never name a variable "dict"--it overwrites a builtin. Commented Oct 20, 2020 at 3:47

1 Answer 1

0

you can try this code and if it not meet your expectation please ping me your exact query....

trading_portfolio={ "Moli": {"Buy": 75, "Sell": 53, "Quantity": 300}, "Anna": {"Buy": 55, "Sell": 83, "Quantity": 154}, "Bob": {"Buy": 25, "Sell": 33, "Quantity": 100}, "Annie": {"Buy": 74, "Sell": 83, "Quantity": 96} } print(trading_portfolio[(list(trading_portfolio.keys())[0])]["Buy"]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.