1

I tried to take the S&P 500 list from wiki. In it, I want to remove some unwanted stocks. My code successfully removed any of the stock in the list EXCEPT the stock 'FOX'.

It is a working code. I just can not remove the stock 'FOX'. Can anyone help me?

import bs4 as bs
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests

# Save S&P 500 to pickle (Source: Wikipedia)
def save_sp500_tickers():
    resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup = bs.BeautifulSoup(resp.text, 'lxml')
    table = soup.find('table', {'class': 'wikitable sortable'})
    tickers = []
    bad_chars = ['\n']
    for row in table.findAll('tr')[1:]:
        ticker = row.findAll('td')[0].text
        tickers.append(ticker)
    tickers = [s.replace('\n', '') for s in tickers] # Remove '\n' from the string
    tickers = [s.replace('.', '-') for s in tickers] # Replace '.' by '-'
    remove_list = ['CTVA', 'DOW', 'FOXA', 'FOX']
    [tickers.remove(s) for s in tickers if s in remove_list]
    print(tickers)
    return tickers

save_sp500_tickers()
Dung Dao
  • 51
  • 1
  • 4
  • Don't remove items from lists while iterating over them – Tim Pietzcker Mar 07 '20 at 12:22
  • FOXA gets removed then the loop skips over FOX... bad idea to remove from a list while looping. Better to copy the ones you want to a new list. – parap Mar 07 '20 at 12:25
  • @TimPietzcker: Hi, Could you give me more explanation and/or give me a suggested method to fix this piece of code? – Dung Dao Mar 07 '20 at 12:27
  • Did you read the question and accepted answer I linked to? This is one of the most common problems Python beginners face (so don't feel bad :)), and it's also one of the most-often asked questions here. – Tim Pietzcker Mar 07 '20 at 12:29
  • @parap: Only the FOX does not get removed. If I replace the FOX by any of the stock. It works. Could you give me a suggested fix for this? Thank you. – Dung Dao Mar 07 '20 at 12:31
  • I bet that in the original list, FOX and FOXA are adjacent to each other. If you choose another stock that's adjacent to another, the problem will return. Did you try the solution from the linked accepted answer? – Tim Pietzcker Mar 07 '20 at 12:34
  • @TimPietzcker: Thank you, I changed the method and it worked now. Yes you are right. I am a very beginner and I am not fond of python so far haizz. I usually use Fortran and lots of stuffs does not make sense to me such as indexing in python. – Dung Dao Mar 07 '20 at 12:43
  • Hang in there, you'll grow to love it, I'm sure. It's not easy to get into the Python mindset if you're used to highly procedural languages, but it's worth it. – Tim Pietzcker Mar 07 '20 at 12:44

0 Answers0