Tanmay
Tanmay

Reputation: 69

function that replaces consecutive duplicate elements of list with single element

What I tried :

def compress(l):
    i = 0
    while i < len(l)-1:
        if l[i] == l[i+1]:
            del l[i]
        else:
            i = i+1
l = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5]
compress(l)

I do not know many functions in python yet as I have just started so I would like to do this the basic way i.e using for and while loops and some list methods. What am I doing wrong? Any other methods Another one I tried what's wrong in this :

def compress(l):
    for i in l:
        if l[i] == l[i+1] and i != (len(l) - 1):
            l.pop(l[i])

        print(l)
l = [1,1,1,1,2,2,2,2,2,2,3,3,3,4,5,6,7,8]
compress(l)

which gives me the output :

[1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8]
[1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8]

Upvotes: 1

Views: 126

Answers (3)

paxdiablo
paxdiablo

Reputation: 881523

You don't appear to be doing anything wrong(a) with your first attempt, other than the fact you're not printing the compressed list. That can be fixed by adding this as the last line:

print(l)

At that point, you should see:

[1, 2, 3, 4, 5]

like I do.


Your second attempt is problematic - it's a common occurrence when you modify a list that you're iterating over. Because the iteration effectively examines based on the index, inserting items before the current point can result in items being processed twice. Additionally, deleting items at or before the current point can result in items not being processed at all.

That latter case is what's happening in your second attempt.


(a) You could probably choose more descriptive names for your variables than l but that's just a preference of mine.

Upvotes: 3

user13053610
user13053610

Reputation:

You don't need to try so hard to delete duplicate elements in a list:

print(list(set(l)))

this will delete all the duplicate elements in the list.

Upvotes: 0

user2390182
user2390182

Reputation: 73460

Since you are asking for other methods, one should point out that repeated removal from a list has bad performance, as all the tail elements have to be shifted for each removal. Building the compressed list from scratch is less expensive and slice assignment allows you to mutate the original list. Using basic loops and list methods, I would do:

def compress(l):
    new_l = l[:1]
    for x in l:
        if x != new_l[-1]:
             new_l.append(x)
    l[:] = new_l

For a one-line alternative using some more advanced means (itertools.groupby), you can do:

from itertools import groupby

def compress(l):
    l[:] = [k for k, _ in groupby(l)]

Upvotes: 1

Related Questions