1

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)
8
  • 1
    you are iterating over tuples.... Do you might want to do something like: 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 parts Commented Nov 17, 2016 at 18:46
  • 1
    it looks like it's working: repl.it/E10r/0 Commented Nov 17, 2016 at 18:47
  • 1
    @Fallenreaper That's not the issue with their code; their code lets i be the whole tuple and access its parts by i[0] and i[1]. Tuple unpacking isn't necessary Commented Nov 17, 2016 at 18:48
  • Maybe there is something wrong with my interpreter... every time I try to run this my second loop produces a blank sectorStocks list. Going to reboot and see if anything changes. Commented Nov 17, 2016 at 18:50
  • Yeah, try, I just ran their code and there are no errors. When I run the code and add a print("List: %s" % sectorStocks) it is actually recomputing and setting the sector stocks accordingly Commented Nov 17, 2016 at 18:50

1 Answer 1

1

Seems to work here.

Perhaps you want a groupBy?

from itertools import groupby
from operator import itemgetter

etfStocks = [('AAPL', 'Technology'), ('FB', 'Technology'), ('SPG', 'Real Estate')]

sectorList = ('Technology', 'Real Estate', 'Industrials')

stocks_by_sector = {k:list(v) for k,v in groupby(etfStocks, itemgetter(1))}

for sector, stocks in stocks_by_sector.items():
  print(sector, stocks)

https://repl.it/E10r/1

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - the error was from my etfStocks list being emptied after the first pass. I added the following and now it works: etfStocks = grabTickers(marketIndex)etfStocks = [i for i in etfStocks]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.