1

So I have a function which returns a List which contains either empty lists or Series. I loop through a list of tickers and for each it will return a empty list or Series and store them inside one list.

However, after looping through all I want to be able to drop the empty lists and only have the Series within the list.

def get_revenue_growth(ticker) -> pd.DataFrame:

income_statement_annually = fa.financial_statement_growth(ticker, FA_API_KEY, period="annual")

if  'revenueGrowth' in income_statement_annually.index:
    
    revenue_growth = income_statement_annually.loc['revenueGrowth']
    exchange_df = pd.DataFrame({ticker : revenue_growth})        
    exchange_df.index = pd.to_datetime(pd.Series(exchange_df.index))
    exchange_df = exchange_df[exchange_df.index.year >= 1998]
    exchange_df = exchange_df.sort_index()
    
    print('Getting Revenue Growth for ' + ticker + ': Passed')
    
else:
    print('Getting Revenue Growth for ' + ticker + ': Failed')
    
    exchange_df = []
    
return exchange_df

This is the function I am calling via this:

revenue_growth = [get_revenue_growth(t) for t in tickers]

Here is what the output looks like...

So what I am trying to achieve is to remove all the empty lists. I tried this list2 = [x for x in list1 if x != []] but it did not work.

4
  • 1
    Try: list2 = [x for x in list1 if x] Commented Jun 3, 2021 at 13:53
  • 1
    Or this list2 = [x for x in list1 if len(x)>0] Commented Jun 3, 2021 at 13:55
  • @JonSG I tried this earlier, sadly get the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Commented Jun 3, 2021 at 13:55
  • Let me add the answer Commented Jun 3, 2021 at 13:56

2 Answers 2

1

You can simply solve it via:

list2 = [x for x in list1 if len(x)>0]
Sign up to request clarification or add additional context in comments.

Comments

1

Look at this Example -

mylist = [] 

if len(mylist) == 0:
    del mylist # Deletes the Empty List
else:
    # Do Something else

Modify this piece for your program

Comments

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.