Home > Enterprise >  List index() function stating a string is not in a given list, when it looks like it is
List index() function stating a string is not in a given list, when it looks like it is

Time:12-08

I have been working on a project to school. At this point I have a list containing multiple list with information about a specific stock I have scraped from a financial site. Here is the list variable named csv_data :

csv_data = [['Table', 'Symbol', 'Company', 'OPEN', 'PREVIOUS CLOSE', 'VOLUME', 'MARKET CAP'], ['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B'], ['Most Actives', 'CCL', 'Carnival Corp', '19.00', '18.59', '29,271,792', '$17.6B'], ['Most Actives', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B'], ['Most Actives', 'BAC', 'Bank of America Corp', '44.60', '44.15', '15,844,588', '$367.4B'], ['Most Actives', 'PFE', 'Pfizer Inc', '51.29', '51.48', '15,897,466', '$297.7B'], ['Most Actives', 'NCLH', 'Norwegian Cruise Line Holdings Ltd', '20.78', '20.03', '14,695,511', '$8.0B'], ['Most Actives', 'C', 'Citigroup Inc', '63.09', '62.52', '12,859,826', '$126.6B'], ['Most Actives', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,762,158', '$210.0B'], ['Most Actives', 'TWTR', 'Twitter Inc', '45.08', '44.47', '10,301,235', '$34.1B'], ['Most Actives', 'KO', 'Coca-Cola Co', '54.91', '54.91', '10,240,325', '$229.2B'], ['Gainers', 'AZO', 'Autozone Inc', '1,905.65', '1,879.99', '135,103', '$39.2B'], ['Gainers', 'DVN', 'Devon Energy Corp', '43.21', '42.36', '5,149,907', '$28.2B'], ['Gainers', 'NOW', 'ServiceNow Inc', '627.10', '616.54', '470,638', '$124.1B'], ['Gainers', 'MRO', 'Marathon Oil Corp', '15.91', '15.60', '6,315,893', '$12.1B'], ['Gainers', 'HES', 'Hess Corp', '77.63', '77.16', '729,681', '$23.9B'], ['Gainers', 'CMG', 'Chipotle Mexican Grill Inc', '1,661.14', '1,623.46', '142,430', '$45.3B'], ['Gainers', 'JNPR', 'Juniper Networks Inc', '32.07', '31.45', '1,864,313', '$10.1B'], ['Gainers', 'EMN', 'Eastman Chemical Co', '111.35', '109.34', '540,451', '$14.1B'], ['Gainers', 'FCX', 'Freeport-McMoRan Inc', '38.28', '37.37', '7,933,903', '$54.5B'], ['Gainers', 'MOS', 'Mosaic Co', '35.15', '34.47', '2,467,991', '$13.1B'], ['Losers', 'WU', 'Western Union Co', '17.84', '17.85', '3,847,445', '$6.9B'], ['Losers', 'MKC', 'McCormick & Company Inc', '89.85', '90.11', '924,086', '$21.7B'], ['Losers', 'CLX', 'Clorox Co', '167.79', '168.73', '488,732', '$20.5B'], ['Losers', 'GNRC', 'Generac Holdings Inc', '375.93', '366.68', '895,094', '$25.6B'], ['Losers', 'MRK', 'Merck & Co Inc', '71.94', '73.42', '7,217,967', '$186.6B'], ['Losers', 'DG', 'Dollar General Corp', '227.05', '225.53', '658,546', '$50.4B'], ['Losers', 'LLY', 'Eli Lilly and Co', '246.21', '246.33', '1,070,621', '$237.1B'], ['Losers', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,813,210', '$210.0B'], ['Losers', 'CHD', 'Church & Dwight Co Inc', '93.82', '94.15', '601,514', '$22.2B'], ['Losers', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B']]

I am looking to find the index of csv_data that contains a user entered stock symbol.

Below is my code to attempt that:

import re


x = list()
found_list = list()

user_symbol = input("Enter a stock: ")

for list_ in csv_data:

for string in list_:
    
    x = re.findall(user_symbol, string)

    if len(x) > 0:
        
        break
       
    
if len(x) > 0:
    
    print(list_)
    abc = found_list.extend(list_)
         
    break



print(found_list)
z = found_list.index(x)

print(z)

When I run this code and enter "F" for user_symbol, I get this error:

Enter a stock: F
['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B']
['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-7435bef5228a> in <module>
     28 
     29 print(found_list)
---> 30 z = found_list.index(x)
     31 
     32 print(z)

ValueError: ['F'] is not in list

I am curious as to why it says 'F' is not in found_list when I can see it printed just above. I may be using the index() incorrectly and would appreciate any help!

CodePudding user response:

I am looking to find the index of csv_data that contains a user entered stock symbol.

Something like the below

csv_data = [['Table', 'Symbol', 'Company', 'OPEN', 'PREVIOUS CLOSE', 'VOLUME', 'MARKET CAP'], ['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B'], ['Most Actives', 'CCL', 'Carnival Corp', '19.00', '18.59', '29,271,792', '$17.6B'], ['Most Actives', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B'], ['Most Actives', 'BAC', 'Bank of America Corp', '44.60', '44.15', '15,844,588', '$367.4B'], ['Most Actives', 'PFE', 'Pfizer Inc', '51.29', '51.48', '15,897,466', '$297.7B'], ['Most Actives', 'NCLH', 'Norwegian Cruise Line Holdings Ltd', '20.78', '20.03', '14,695,511', '$8.0B'], ['Most Actives', 'C', 'Citigroup Inc', '63.09', '62.52', '12,859,826', '$126.6B'], ['Most Actives', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,762,158', '$210.0B'], ['Most Actives', 'TWTR', 'Twitter Inc', '45.08', '44.47', '10,301,235', '$34.1B'], ['Most Actives', 'KO', 'Coca-Cola Co', '54.91', '54.91', '10,240,325', '$229.2B'], ['Gainers', 'AZO', 'Autozone Inc', '1,905.65', '1,879.99', '135,103', '$39.2B'], ['Gainers', 'DVN', 'Devon Energy Corp', '43.21', '42.36', '5,149,907', '$28.2B'], ['Gainers', 'NOW', 'ServiceNow Inc', '627.10', '616.54', '470,638', '$124.1B'], ['Gainers', 'MRO', 'Marathon Oil Corp', '15.91', '15.60', '6,315,893', '$12.1B'], ['Gainers', 'HES', 'Hess Corp', '77.63', '77.16', '729,681', '$23.9B'], ['Gainers', 'CMG', 'Chipotle Mexican Grill Inc', '1,661.14', '1,623.46', '142,430', '$45.3B'], ['Gainers', 'JNPR', 'Juniper Networks Inc', '32.07', '31.45', '1,864,313', '$10.1B'], ['Gainers', 'EMN', 'Eastman Chemical Co', '111.35', '109.34', '540,451', '$14.1B'], ['Gainers', 'FCX', 'Freeport-McMoRan Inc', '38.28', '37.37', '7,933,903', '$54.5B'], ['Gainers', 'MOS', 'Mosaic Co', '35.15', '34.47', '2,467,991', '$13.1B'], ['Losers', 'WU', 'Western Union Co', '17.84', '17.85', '3,847,445', '$6.9B'], ['Losers', 'MKC', 'McCormick & Company Inc', '89.85', '90.11', '924,086', '$21.7B'], ['Losers', 'CLX', 'Clorox Co', '167.79', '168.73', '488,732', '$20.5B'], ['Losers', 'GNRC', 'Generac Holdings Inc', '375.93', '366.68', '895,094', '$25.6B'], ['Losers', 'MRK', 'Merck & Co Inc', '71.94', '73.42', '7,217,967', '$186.6B'], ['Losers', 'DG', 'Dollar General Corp', '227.05', '225.53', '658,546', '$50.4B'], ['Losers', 'LLY', 'Eli Lilly and Co', '246.21', '246.33', '1,070,621', '$237.1B'], ['Losers', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,813,210', '$210.0B'], ['Losers', 'CHD', 'Church & Dwight Co Inc', '93.82', '94.15', '601,514', '$22.2B'], ['Losers', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B']]


symbol = 'LLY'
found = False
for idx,e in enumerate(csv_data):
  if e[1] == symbol:
    print(f'{symbol} was found in index {idx}')
    found = True
    break
if not found:
  print(f'Could not find the symbol {symbol}')

CodePudding user response:

For anyone seeing this, I of course finally figure it out after posing the question.

When indexing I was using the x variable to search within the variable found_list.

x is type list at this instance, I instead used x[0] to return the string value.

CodePudding user response:

As the error tells ['F'] is not in the list, and that is right as it is a one element list.

  • found_list does not have an element that is a list with one element being F
  • found_list does have a string being F

That is because re.findall returns a list : found_list.index(x[0]) gives 1


user_symbol = "GNRC"
for idx, (_, symbol, *_) in enumerate(csv_data):
    if symbol == user_symbol:
        print(f'{user_symbol} was found in index {idx}')
        break
else:
    print(f'Could not find the symbol {user_symbol}')
  • Related