I would appreciate if someone would be kind enough to help me figure this out.
In this example I have a list of dictionaries where each dictionary contains various parameters about a stock 'Symbol'. I'm trying to retrieve the 'Quantity' of stocks in the list for a given 'Symbol'. If the 'Symbol' doesn't exist in the list it should return 0.
p = [
{'AveragePrice': '8.5334167347', 'AssetType': 'STOCK', 'Quantity': '124', 'Symbol': 'FNGU'},
{'AveragePrice': '6.5334167347', 'AssetType': 'STOCK', 'Quantity': '100', 'Symbol': 'SPY'},
{'AveragePrice': '7.5215053838', 'AssetType': 'STOCK', 'Quantity': '69', 'Symbol': 'LABU'}
]
def getposition(symbol):
for d in p:
if d["Symbol"] == symbol:
q = int(d["Quantity"])
else:
q = 0
return q
print("LABU", getposition("LABU"))
print("FNGU", getposition("FNGU"))
print("SPY", getposition("SPY"))
print("AAPL", getposition("AAPL"))
But I keep getting false results:
LABU 69
FNGU 0
AAPL 0
SPY 0
What would be the correct way to scan through the list to get the correct results?
Thanks!