If I have a Python list, how can I combine some specific elements from the list?
My string looks like this:
my_string = ['Dog', 'Cat', 'And', 'Mouse', 'Whale', 'Cat', 'And', 'Mouse']
I want my new string to look like this:
new_string = ['Dog', 'Cat And Mouse', 'Whale', 'Cat And Mouse']
Here's my code so far (not correct):
new_string = iter(my_string)
my_string[:] = [i + next(new_string) if i == 'Cat' else i for i in new_string]
If I print my new string, I get:
['Dog', 'CatAnd', 'Mouse', 'Whale', 'CatAnd', 'Mouse']
Where am I going wrong? Thank you.