I’m trying to do the following (consider this list):
['Hello, 'Hi', 'Nice', Cool']
I would like to change ‘Hi’ to ‘Love’
But, I wouldn’t want it to stay that way:
['Hello, 'Love', 'Nice', Cool']
I’m trying to get ahead of the others, even cropping the last one, getting like this:
['Hello, 'Love', 'Hi', Nice']
Note that Hi passed one along with the entire list, that’s what I want!
Anybody know? Thanks in advance!
>Solution :
old_list = ['Hello', 'Hi', 'Nice', 'Cool']
new_item_index = 1
new_item = 'Love'
new_list = old_list[0:new_item_index] + [new_item] + old_list[new_item_index+1:]
print(old_list)
print(new_list)
['Hello', 'Hi', 'Nice', 'Cool']
['Hello', 'Love', 'Hi', 'Nice']