Reputation: 383
I have a list containing ~300 lists, however some of them are duplicates and I would like to remove them. I've tried:
cleanlist = [cleanlist.append(x) for x in oldlist if x not in cleanlist]
but it keeps throwing RuntimeError: maximum recursion depth exceeded in comparison
at me. I tried sys.setrecursionlimit(1500)
but that didn't help.
What's a better way to do this?
Upvotes: 0
Views: 3832
Reputation: 6670
A crazy solution. Maybe It helps:
oldlist = [[1,2,3], [1,2,3], [4,5,6], [7,8], [7,8]]
cleanlist = set(tuple(x) for x in oldlist)
print(list(cleanlist))
>>> [(7, 8), (4, 5, 6), (1, 2, 3)]
For a list of lists, use this:
cleanlist = [list(item) for item in set(tuple(x) for x in oldlist)]
>>> [[7, 8], [4, 5, 6], [1, 2, 3]]
Upvotes: 1
Reputation: 49320
You're adding a bunch of stuff to cleanlist
based on whether it's in cleanlist
(which doesn't exist yet), and then saving the value returned from the append()
operation (which is None
) to cleanlist
. It will not end well.
A better way to do this is probably with an old-fashioned loop construct:
cleanlist = []
for x in oldlist:
if x not in cleanlist:
cleanlist.append(x)
Upvotes: 6