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"
martineau
  • 119,623
  • 25
  • 170
  • 301
ammely
  • 85
  • 1
  • 8
  • 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. – ggorlen Oct 20 '20 at 03:47

1 Answers1

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"])