0
df_store_index_list = df_store.index.tolist()
df_store_column_list = df_store[column].tolist()

list_to_be_returned = []

for i in range(len(df_store_index_list)):
    list_to_be_returned.append([df_store_index_list[i], df_store_column_list[i]])

# return list_to_be_returned
return [[df_store_index_list[i], df_store_column_list[i]] for i in range(len(df_store_index_list)) ] not working!!!!

I have a function that returns a two-dimensional list.

Problem: the list comprehension on the last line is giving me an error saying "df_store_index_list is not defined".

Solution: I created my own list (list_to_be_returned) and did a custom for loop and it's working fine. It has a value (list_to_be_returned). But I was just wondering, why is the list comprehension not working?

here is the complete code

@classmethod
def store_specific_info_string(cls, store_name, column, ascending=False):
    """
    Brief
        - filter for specific store

    Description
        - obtain sum of column based on specific `Store_Name`

    Parameter
        - store_name    : inside the `Store_Name` column
        - ascending     : True or False
        - column        : sum of what column? (Total_Sales, Total_Profit)

    Return Value(s)
        - tuple of name(Item_Description) and sum of column passed based on name.
    """
    # filter the store by store name
    df_store = cls.dataframe[ cls.dataframe[ "Store_Name" ] == store_name]

    df_store = df_store.groupby("Item_Description").sum()[[column]]
    # sort them by the column(integer)
    df_store.sort_values(column,ascending=ascending ,inplace=True)

    df_store_index_list = df_store.index.tolist()
    df_store_column_list = df_store[column].tolist()

    list_to_be_returned = []

    for i in range(len(df_store_index_list)):
        list_to_be_returned.append([df_store_index_list[i], df_store_column_list[i]])

    return list_to_be_returned
    # return [[df_store_index_list[i], df_store_column_list[i]] for i in range(len(df_store_index_list)) ] not working!!!!

here is a pdb initiated inside pdb

Christian
  • 33
  • 1
  • 8

1 Answers1

0

Based on your comment it seems that you really want the list of list format with a list comprehension. Here is another way to do your list comprehension (but that doesn't explain why yours didn't work)

column_serie = df_store[column]
[[idx, value] for idx,value in column_serie.iteritems()]

abcdaire
  • 1,528
  • 1
  • 10
  • 21
  • in this case, the variable "column" is now undefined. – Christian Jul 23 '19 at 22:20
  • 1
    you should be able to reference `df_store_index_list` from within that list comprehension, i'm not sure what the scope has to do here... – juanpa.arrivillaga Jul 23 '19 at 22:24
  • Agree with @juanpa.arrivillaga: List comprehensions have their own scopes, but it's a closure scope; they can load `df_store_index_list` (and anything else from the outer scope) just fine. Not allowing that would have caused all sorts of problems in the transition from Python 2 (where listcomps didn't have their own scope, and read and write to the scope they run in like any other loop). – ShadowRanger Jul 26 '19 at 16:23
  • In fact after testing it appears that it should work perfectly , I will update the answer leaving only the list comprehension part. – abcdaire Jul 26 '19 at 16:39