-2
{
 'Key1':{ 'price': 40,
        'amount': 30
       },
 'Key2':{ 'price': 50,
        'amount': 40
       },
 'Key3':{ 'price': 70,
        'amount': 50
       }
}

How to get all the prices?

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30

2 Answers2

1

Here is one way to do it:

d={
 'Key1':{ 'price': 40,
        'amount': 30
       },
 'Key2':{ 'price': 50,
        'amount': 40
       },
 'Key3':{ 'price': 70,
        'amount': 50
       }
}

prices=[k['price'] for i,k in d.items()]

print(prices)

Output:

[40, 50, 70]
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
-1

If the dictionary is called dict1, use:

[k['price'] for k in dict1.values()]
anvoice
  • 295
  • 2
  • 7