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] x in inpt: if x not in output: output.append() return output so trying figure out how use function above 2 vars(output/input) input has list of numbers , output empty @ end should have list of unique numbers excluding dup in inpt list. when run program getting "process finished exit code 0 " believe means program executed not seeing output return output @ end of script. appreciated.
there 3 problems code.
you have call function in order see output. add
print(rem_dups())@ end.when do, typeerror because not pass append method. change
output.append(x)the algorithm use o(k*k) k number of unique entries, because of linear search in
x not in output. unnecessarily slow , bite large inputs. let assume want output list order preserved.
here fix.
def rem_dups(): output = [] uniques = set() inpt = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6] x in inpt: if x not in uniques: uniques.add(x) output.append(x) return output print(rem_dups())
Comments
Post a Comment