How can I get the price value with the get() function instead of using []?
As you can see availableToBack and availableToLay dictionaries can be empty at one point and I am trying to prevent an error when it happens and return None.
u'ex':
{u'availableToBack':
[
{u'price': 1.02,
u'size': 2.15}
],
u'availableToLay': [],
u'tradedVolume': []
}
The way I get price is:
price = ['ex']['availableToBack'][0][price]
But in case of availableToLay it would give a key error. I know there is another way which would give None if value does not exist.
price = get('ex')...
Just can't figure out how construct this complex version of it. Can you hep?
price = ['ex']['availableToBack'][0][price]withprice=ex.get('availableToBack').get(0).get(price)this should do what you want