Abdul Fayad
Abdul Fayad

Reputation: 11

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:
            output.append()
    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.

Upvotes: 0

Views: 64

Answers (3)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

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:
            uniques.add(x)
            output.append(x)
    return output

print(rem_dups())

Upvotes: 2

Håken Lid
Håken Lid

Reputation: 23064

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)

Upvotes: 0

jonDO
jonDO

Reputation: 1

inpt = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6]
inpt=list(set(inpt))

this set() function removes dublicates

Upvotes: 0

Related Questions