views:

76

answers:

1

I want to remove the duplicate elements in a list only when one element is repeated many times, like this:

li = ['Human','Human','Human'] => li = ['Human']

but not when there are two or more different elements:

li = ['Human','Monkey','Human', 'Human']

Many thanks in advance.

+3  A: 
def clean(lst):
    if lst.count(lst[0]) == len(lst):
        return [lst[0]]
    else:
        return lst

Does that do what you want?

if so, then you can do it in place as well

def clean_in_place(lst):
    if lst.count(lst[0]) == len(lst):
        lst[:] = [lst[0]]
aaronasterling
That works great! btw was how do you clean duplicates if the successive elements are same, like ['a','a','a','b','a','a'] to ['a','b','a']
DGT
Just iterate through the list..
poke