-2

I want to return a list of items from a sequence without any elements with the same value next to each other and preserving the original item order.

I've written a solution that works but my attempt to make this solution more pythonic does not work. It seems the if statement isn't working; it just returns the whole original sequence in a list.

The solution should be a function and I don't want to use imports.

Def order(iterable):
    iter_list = list(iterable)
    item_list = []
    item_list.append(iter_list[item] for item in range(0, len(iter_list)) if iter_list[item] != iter_list[item -1])
    return iter_list

I just need to know why (if iter_list[item] != iter_list[item -1) is not working

Ekemi
  • 3
  • 3
  • 2
    Can you provide a [MRE]? – Michael Szczesny Dec 19 '21 at 20:28
  • `append` makes no sense here. You add one item to your list and that is a generator that would create the data if you iterate over it (e.g. `print(list(item_list[0]))`). If you want a list comprehension it would be `item_list = [iter_list[item] for item in range(0, len(iter_list)) if iter_list[item] != iter_list[item - 1]]` and you will have to return `item_list` of course. – Matthias Dec 19 '21 at 20:49
  • Thanks @Matthias just what i needed. – Ekemi Dec 19 '21 at 21:05

2 Answers2

0

If you zip the list with the same list starting at index 1 you're quite close to your goal but you might miss the last element.

data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip(data, data[1:]) if x != y]
print(result)

This gives you [0, 1, 2, 3, 4, 5, 6, 7], but we would expect an additional 8 in the list.

To get the last value too we can use itertools.zip_longest.

from itertools import zip_longest

data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip_longest(data, data[1:]) if x != y]
print(result)

This results in [0, 1, 2, 3, 4, 5, 6, 7, 8].

If you don't want to duplicate the list then use islice.

from itertools import islice, zip_longest

data = [0, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8]
result = [x for x, y in zip_longest(data, islice(data, 1, None)) if x != y]
print(result)
Matthias
  • 12,873
  • 6
  • 42
  • 48
0

Your original code works just fine if you just take what's inside append if we somehow account for the first element. One way is:

out = [iter_list[item] for item in range(0, len(iter_list)) if (item==0 or iter_list[item] != iter_list[item -1])]
  • Preservation of original order is prerequisite in the question. – DarkKnight Dec 19 '21 at 20:40
  • 2
    Your `set` solution breaks for `[1,2,1]`. – Michael Szczesny Dec 19 '21 at 20:42
  • 1
    `[i for i,j in zip(lst,lst[1:]) if i!=j]` will not work if the last item isn't a duplicate from the item before. – Matthias Dec 19 '21 at 20:43
  • After your 5th edit: this fails for `[1, 2, 1]`, it returns `[2, 1]`. – Thierry Lathuille Dec 19 '21 at 20:55
  • @ThierryLathuille I realized that. I think it works for `[1,2,1]` now. Wow what a mess. Sorry about that. –  Dec 19 '21 at 21:00
  • A solution like the current one is like saying: "OK, there's a big hole in the floor, but we put a carpet over it so everything looks fine". Yes, the result looks OK, but the real fix would be to close the hole which in this case means: change the algorithm. (This comment might not be valid any more at the time you read it since the answer keeps changing). – Matthias Dec 19 '21 at 21:01
  • @Matthias Do you mean the hole that swallows the `None` from input `[None]` when using *your* solution? :-P – Kelly Bundy Dec 19 '21 at 21:04
  • @ThierryLathuille yes I need to work on this, I think the 1st element of every sequence should be returned. thanks – Ekemi Dec 19 '21 at 21:06
  • @KellyBundy I had a feeling someone would come up with this but I didn't want to overcomplicate things. Throw in a `fillvalue=object()` as the last parameter to `zip_longest` and you're fine. – Matthias Dec 19 '21 at 21:09
  • @Matthias Still swallows [the Borg](https://tio.run/##TY2xbsMwEEN3fQW7SYCXIksQwEu/oVtgCEpySlUoOuV0Duz@vBshSzhwIMHHuuoPl92@yrZF4RuSkihzbki3yqL4S9VnLldqasw5h9bwxXI9GDx1oQjv6e69bZTjgFBWLuRebZeQzlLwLTMZcwkaMOLYAdZNRqjNWXuyILJgGbAilfdP2zcDuh8/D9OAmHJ@hDzTyKdfOqt1DiliwceIdTJVUlH7Artt@wc). – Kelly Bundy Dec 19 '21 at 21:15