Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning list results in none

So i am working on a little program to remove duplicates from a given file through a GUI, to learn how to make GUI's with Python.

I have written a method that should take a string, turn it into a list, remove duplicates from the list and it actually does this. The problem occurs when i want to return the result, because if i print() the returned value it just results in None being printed. But if i print() the value i want to return inside the method it prints out the right list.

The class looks like so:

#Class that removes the duplicates
class list_cleaner():
    def __init__(self):
        self.result_list = []

    def clean(self,input_string, seperator, target):
        #takes a string and a seperator, and splits the string at the seperator. 
        working_list = self.make_list_from_string(input_string,seperator)

        #identify duplicates, put them in the duplicate_list and remove them from working_list 
        duplicate_list = []
        for entry in working_list:
            instances = 0
            for x in working_list:
                if entry == x:
                    instances =  instances + 1
            if instances > 1:
                #save the found duplicate
                duplicate_list.append(entry)
                #remove the duplicate from working list
                working_list = list(filter((entry).__ne__, working_list))

        self.result_list = working_list + duplicate_list 
        print(self.result_list) #Prints the result list
        return self.result_list

The main function looks like so (Note: duplicate_remover is the facade for list_cleaner):

if __name__ == "__main__":
    remover = duplicate_remover()
    x = remover.remove_duplicates("ABC,ABC,ABC,DBA;DBA;DBA,ahahahaha", ",")
    print(x) #Prints none. 

TL;DR:

I have a method f that returns list l which is an attribute of a class C.

If i print() l as a part of f the values of l is being printed.

If i return l and store it in a variable outside the scope of f, and then print() this variable it prints None.

Thanks in advance!

Edit 1:

The duplicate_remover code was requested. It looks like so:

class duplicate_remover():
    def remove_duplicates(self,input_string,seperator):
        my_list_cleaner = list_cleaner()
        my_list_cleaner.clean( input_string = input_string, seperator = seperator)
like image 638
TheInternetIsNeat Avatar asked Nov 26 '25 22:11

TheInternetIsNeat


1 Answers

remove_duplicates forgets to return the return value of my_list_cleaner.clean(...), which leads to the default value None being returned.

like image 59
timgeb Avatar answered Nov 28 '25 10:11

timgeb