# list - 在python中，如何根据前置项移除列表项

``````
[(1, 'a'), (2, 'b'), (2, 'b'), (2, 'c'), (3, 'd'), (2, 'e')]

``````

``````
[(1, 'a'), (2, 'b'), (3, 'd'), (2, 'e')]

``````

``````
mylist = [...]

for i in range(len(mylist)):

if mylist[i-1].attr == mylist[i].attr:

mylist.remove(i)

``````

``````
from itertools import groupby

from operator import itemgetter

data = [(1, 'a'), (2, 'a'), (2, 'b'), (3, 'a'), (4, 'a'), (2, 'a'), (2, 'a'), (3, 'a'), (3, 'a')]

[next(group) for key, group in groupby(data, key=itemgetter(0))]

``````

``````
[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a'), (2, 'a'), (3, 'a')]

``````

``````
result = []

for first, second in zip(data, data[1:]):

if first[0]!= second[0]:

result.append(first)

result

``````

``````
[(1, 'a'), (2, 'b'), (3, 'a'), (4, 'a'), (2, 'a')]

``````

``````
l = [(1, 'a'), (2, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]

from itertools import groupby

[tuple(k) for k, _ in groupby(l)]

# [(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]

``````

``````
test = [(1, 'a'), (2, 'a'), (2, 'a'), (3, 'a'), (4, 'a'),(3, 'a'),(4,"a"),(4,"a")]

result = []

for i in test:

if result and i[0] == result[-1][0]: #edited since OP considers (1,"a") and (1,"b") as duplicate

#if result and i == result[-1]:

continue

else:

result.append(i)

print (result)

``````

``````
[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a'), (3, 'a'), (4, 'a')]

``````

``````
inputList = [(1, 'a'), (2, 'a'), (2, 'a'), (3, 'a'), (2, 'a')]

outputList = []

lastItem = None

for item in inputList:

if not item == lastItem:

outputList.append(item)

lastItem = item

print(outputList)

``````