Removing Consecutive Duplicates in Lists
In the following a simple and short function is presented that removes any consecutive duplicates in an arbitrary list.
Result:
['1', '2', '3', 'a', 'b', 'c', 'd']
In the following a simple and short function is presented that removes any consecutive duplicates in an arbitrary list.
def RemoveConsecutiveDuplicates(theList):
return [item for index, item in enumerate(theList) \
if index==0 or item != theList[index-1]]
example = ['1','1','2','3','3','a','a','b','b','c','c','c','c','d']
print(RemoveConsecutiveDuplicates(example))
Result:return [item for index, item in enumerate(theList) \
if index==0 or item != theList[index-1]]
example = ['1','1','2','3','3','a','a','b','b','c','c','c','c','d']
print(RemoveConsecutiveDuplicates(example))
['1', '2', '3', 'a', 'b', 'c', 'd']
Copyright ©2017, Software Developer, All rights reserved.
See Contents
No comments:
Post a Comment