I've a list like:
mylist = ['La', 'domestication', "d'un", 'animal', 'ou', "d'un", 'végétal,', 'necessite', "l'acquisition", "d'une", 'ferme']
I want to split elements which have " ' " inside by 2 elements and keep their index in the original list.
OUTPUT REQUESTED : my_new_list = ['La', 'domestication', "d'" ,'un', 'animal', 'ou', "d'", 'un', 'végétal,', 'necessite', "l'", 'acquisition', "d'", 'une', 'ferme']
I've tried few thing but I admit I'm stuck to replace the two new split element in the correct index, here is the code I've tried:
for word in mylist:
if "'" in word:
new_words = word.split("'")
mylist[mylist.index(word)] = (new_words[0]+"'")
mylist.insert(mylist.index((new_words[0]+"'")+1), new_words[1])
print(mylist)
Thank you for your time and help :)
listcan be trivially tweaked to alter the originallist(for when you're relying on aliases of thelistseeing the modification), so it hardly matters which solution is used (aside from the minor memory expense of having two at once). Just changemy_new_list = operation_on(mylist)tomylist[:] = operation_on(mylist)and it swaps out the contents in-place (then discards the temporary).