Is there more pythonic way to remove all duplicate elements from a list, while keeping the first and last element?
lst = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
occurence = [i for i, e in enumerate(lst) if e == "foo"]
to_remove = occurence[1:-1]
for i in to_remove:
del lst[i]
print(lst) # ['foo', 'bar', 'foobar', 'barfoo', 'foo']
Another approach which I like more.
for i, e in enumerate(lst[1:-1]):
if e == "foo":
del lst[i 1]
CodePudding user response:
You can construct a new list with your requirements using Python's set data structure and slicing like this:
lst = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
new_lst = [lst[0]] list(set(lst[1:-1])) [lst[-1]]
CodePudding user response:
Try below:
lst = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
first, last = lst[0], lst[-1]
seen = {first, last}
seen_add = seen.add
inner = (s for s in lst[1:-1] if not (s in seen or seen_add(s)))
result = [first, *inner, last]
Result:
['foo', 'bar', 'foobar', 'barfoo', 'foo']
CodePudding user response:
You can try this:
mylist = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
none_duplicated = list(dict.fromkeys(mylist))
print([mylist[0], *none_duplicated, mylist[-1]])
Actually, your question is not about to remove duplicates, but remove the occurrences of your first item in the list (assume first and last are the same). But if you want a more generic way, you will first try to get a list of none duplicated value, and then add your first and last.
CodePudding user response:
According to your example, it's necessary to remove the duplicates looking at the entire list and then adding the first and last elements:
lst = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
data = [lst[0],
*set([w for w in lst if w not in [lst[0], lst[-1]]]),
lst[-1]]
print(data)
Output:
['foo', 'bar', 'barfoo', 'foobar', 'foo']
CodePudding user response:
Use a slice-assignment and unpack/consume a filter?
lst = ["foo", "bar", "foobar", "foo", "barfoo", "foo"]
lst[1:-1] = filter(lambda x: x != "foo", lst[1:-1])
print(lst)
Output:
['foo', 'bar', 'foobar', 'barfoo', 'foo']