I have a program that loads of a list of stock tickers and sectors into a list called etfStocks. It looks like the following:
etfStocks = [('AAPL', 'Technology'), ('FB', 'Technology'), ('SPG', 'Real Estate'), ...]
I then have another list where I have each sector loaded into list called sectorList. It looks like the following:
sectorList = ('Technology', 'Real Estate', 'Industrials', ...)
I'm trying to iterate over the sectorList list and load each of the tickers for a given sector into their own list, which I then perform analysis on. The script below works perfectly for the first pass, but when the loop goes into the second sector, it never rebuilds the sectorStock list. I've tried switching between the code below and nest for loops, but nothing seems to work.
Does anyone have any idea why it won't re-iterate over the nested loop?
sectorStocks = []
for sector in sectorList:
sectorStocks.clear()
sectorStocks = [i[0] for i in etfStocks if i[1] == sector]
print("Computing statistics for: %s" % sector)
sectorStocks = [a for a,b in etfStocks if a == sector]Since you are looping over etf stocks here, you need a reference to both tuple partsprint("List: %s" % sectorStocks)it is actually recomputing and setting the sector stocks accordingly