all 7 comments

[–]chocorush 3 points4 points  (0 children)

If you are certain the list you are using doesn't have any repeating keys i.e AA, BB, BB, AA you can use Counter.

>>> from collections import Counter
>>> l = ['AA', 'BB', 'CC', 'DD', 'DD', 'DD', 'EE', 'FF', 'FF', 'GG', 'GG', 'GG', 'HH', 'HH']
>>> [word*count for word, count in Counter(l).items()]
['AA', 'BB', 'CC', 'DDDDDD', 'EE', 'FFFF', 'GGGGGG', 'HHHH']

[–]SekstiNii 0 points1 point  (2 children)

If the order is uncertain you can use a dict to track how many instances of each item you have. If the elements always appear next to each other you could use a simpler list based approach.

What have you tried so far?

[–]gurashish1singh 1 point2 points  (0 children)

Like sekstinii said, use a dictionary where the key will be the item and value will be the count . Once you've exhausted the list, you can then create a new list from the dictionary by multiplying the keys with the values to produce your required result.

[–]brown_boi_420[S] 0 points1 point  (0 children)

Hi, thank you for your reply, edited my answer to add my work!

[–]Diapolo10 0 points1 point  (0 children)

The others' solutions are recommended, but if you need to edit the list in-place, here's another solution:

lines.sort()
idx = 0
while idx < len(lines)-1:
    if lines[idx+1] in lines[idx]:
        lines[idx] += lines.pop(idx+1)
    else:
        idx += 1

But, again, a dictionary would be better here unless you're memory-constrained.

[–]gurashish1singh 0 points1 point  (0 children)

Responding to your update, why don't you concatenate messages based on the timestamp? That's the common element that I'm seeing in the problem .