I have a list in a particular format as follows:
my_list = ['apple', 'apple', 'boy', 'cat', 'cat', 'apple', 'apple',
'apple', 'boy', 'cat', 'cat', 'dog', 'dog'].
And my expected output is
res = ['apple', 'boy', 'cat', 'apple', 'boy', 'cat', 'dog']
The consecutive occurrence of the same word should be replaced with the word only once irrespective of whether the word occurred as another sequence earlier.
The following code when I used gives the following output.
test_list = ['apple', 'apple', 'boy', 'cat', 'cat', 'apple', 'apple',
'apple', 'boy', 'cat', 'cat', 'dog', 'dog']
res = []
[res.append(x) for x in test_list if x not in res]
print ("The list after removing duplicates : " + str(res))
output: ['apple', 'boy', 'cat', 'dog'] - which gave only distinct words. How will I proceed from here to get what I actually require. Thanks in advance.