Home Ask Login Register

Developers Planet

Your answer is one click away!

Abdul Fayad February 2016

python removal of duplicate entries in list and print output of unique list

def rem_dups():
    output = []
    inpt = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6]
        for x in inpt:
            if x not in output:
    return output

So trying to figure out how to use my function above with 2 vars(output/input) where the input has the list of numbers and output is empty but at end should have list of unique numbers excluding any dup in the inpt list. When i run this program i am getting "Process finished with exit code 0 " which i believe means program executed properly but not seeing any output from the return output at end of script. Any help would be appreciated.


jonDO February 2016

inpt = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6]

this set() function removes dublicates

Håken Lid February 2016

You are correct about Process finished with exit code 0. That means your script was executed with no errors.

When running a script from the command line like so $ python myscript.py, the only output will be whatever is printed to stdout. Python's return doesn't return anything to the shell, it only works inside python.

Insted you can print to std that by simply calling print(output), at the end of your script or main() function. (Or anywhere else)

Terry Jan Reedy February 2016

There are three problems with your code.

  1. You have to call the function in order to see any output. Add print(rem_dups()) at the end.

  2. When you do, you will get a TypeError because you do not pass anything to the append method. Change to output.append(x)

  3. The algorithm you use is O(k*k) where k is the number of unique entries, because of the linear search in x not in output. This is unnecessarily slow and will bite for large inputs. Let us assume that you want output to be a list with order preserved.

Here is a fix.

def rem_dups():
    output = []
    uniques = set()
    inpt = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6]
    for x in inpt:
        if x not in uniques:
    return output


Post Status

Asked in February 2016
Viewed 3,082 times
Voted 11
Answered 3 times


Leave an answer

Quote of the day: live life