Say I have a basic list.
x = [2,3,1,3,5,4,5]
And I want to find the sum result applied against the list without duplicates.
So with duplicates the code could be.
[elem * 2 for elem in x if elem >= 1 ]
> [2, 4, 6, 6, 8, 10]
But I don't want duplicates. So my thought was I should make a copy of the list filtered.
# so for elements in x not equal to any other elements in the list * 2.
#So first sort the list
#Then remove any items that are equal to the next item in the list. So items not equal to the next should be kept.
x = [2,3,1,3,5,4,5]
x.sort()
y =[]
y = [elem for elem in x if x != elem[::1]]
I get an error that it is not subscriptable.
TypeError: 'int' object is not subscriptable
Thought this altered version might work but still same error.
y = [elem for elem in x if elem != (elem[::1])]
I found this in the docs for removing duplicates but it seems rather unweildy for such a small thing.
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-2, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
There is a few cool ideas at activestate http://code.activestate.com/recipes/5256 for example Raymonnd H with
def uniq(alist) # Fastest order preserving
set = {}
return [set.setdefault(e,e) for e in alist if e not in set]
But I am just darn curious why I get a subscriptable error.