I have a list of integers and want to append data after a certain element in the list. I know about the list function, but when I go to use it in a loop, it appends the same data in the same position x
amount of times.
Here is what I have:
lister = [1, 2, 3, 4, 5]
counter = 0
for i in range (len(lister)):
lister.insert(i, "Hello")
print(lister)
When I run it, I get ['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 1, 2, 3, 4, 5]
.
How would I make it so that when I run it, I get, [Hello, 1 , Hello, 2, ...]
and so on?
Small change:
lister.insert(i*2, "Hello")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With