I have a function, unique(a)
that takes a list, a
, of numbers and returns only one of each value. At the same time, it maintains the order of the list. I also have a function, big_list(n)
that generates a list of len(n)
.
The reason to why I reverse the direction of the list is so that when removing values, It removes them from the back of the original list, just to make the modified list more clean and readable when comparing it to the original list.
The function works when I have a relatively small length of the list I'm creating, but when I get to larger lengths, such as 1,000,000 for ex, the execution time takes FOREVER.
If anyone can help me by making my function a lot faster, that would be great!
FYI : I need to use a set somewhere in the function for the assignment I am working on. I still need to remove list items from the back as well.
Thanks in advance!
def big_list(n) :
# Create a list of n 'random' values in the range [-n/2,n/2]
return [ randrange(-n//2, n//2) for i in range(n) ]
def unique(a) :
a = a[::-1]
b = set(a)
for i in b :
while a.count(i) != 1 :
a.remove(i)
a.count(i)
a = a[::-1]
return a