Home > Net >  Python: handling NoneType objects when appending to a list
Python: handling NoneType objects when appending to a list

Time:09-17

I am appending data to a list from a dictionary i have scraped. One element is the exchange rate for a given stock. In some cases there is no exchange done on the given stock, so this element is not provided in the dictionary I am scraping and returns 'None'.

I am trying to introduce an if statement in my for loop to append the value 1 (could be anything, it doesn't matter) if the object is NoneType however I keep getting the same error.

data_list=[]

for item in data['data']:
        #print(item)
    order_details = trading212.get_order_details(item['detailsPath'])
    #print(order_details)
    print(item['heading']['context']['prettyName'])
    #print(order_details['sections'][3]['rows'][1]['value']['context']['amount'])
    
    data_list.append([item['heading']['context']['prettyName'], #Name
                      item['heading']['context']['instrument'], #Ticker
                      item['subHeading']['context']['quantity'], #Qty
                      item['mainInfo']['context']['amount'], #Amount paid
                      item['mainInfo']['context']['currency'], #Currency paid
                      
                      order_details['sections'][3]['rows'][0]['value']['context']['amount'], #exchange_fee
                      order_details['sections'][2]['rows'][1]['value']['context']['quantity'], #filled qty
                      order_details['sections'][2]['rows'][2]['value']['context']['amount'], #Stock Price/ Fill Price
                      order_details['sections'][2]['rows'][2]['value']['context']['currency'], #Currency of Stock
            
                      item['date']])
    
    if order_details['sections'][2]['rows'][4]['value']['context']['quantity'] is not None:
        data_list.append(order_details['sections'][2]['rows'][4]['value']['context']['quantity']) #exchange_rate)
    else: 
        data_list.append(1)

I get the following error message:

if order_details['sections'][2]['rows'][4]['value']['context']['quantity'] is not None:
TypeError: 'NoneType' object is not subscriptable

CodePudding user response:

if order_details['sections'][2]['rows'][4]['value']['context']['quantity'] is not None:

means that the value of the dictionnary "quantity" inside your big dictionnary of dictionnary of dictionnary.. is not None, but the dictionnary "quantity" has to exist for this to work (which doesn't seem to be the case)

if you want to check if quantity exists, you first have to do that:

if "quantity" in order_details['sections'][2]['rows'][4]['value']['context'].keys():

which checks if the key "quantity" exists in your dictionnary in the first case

I think that should sove your problem, but it would be intresting to see the structure of your variable in case it doesn't

EDIT: Since you're using different slice then before, it's hard to tell at which point the key doesn't exist in the dictionnary (it even looks to be actually earlier then I thought)

For you to check wether quantity exists, you have to be sure that previous dictionnary exists as well, essentially you have to check if context exists also, value also..

Try to have a consistent dictionnary would be easier for you to verify value, but if you know to which point the value cease to exist, that's where you have to check

CodePudding user response:

You can always check if the object is of None type or not before

if obj is not None:
  print(obj)

Get more details here

  • Related