Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Append Data After A Certain Element in a List

Tags:

python-3.x

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?

like image 939
Hasnain Ali Avatar asked Mar 05 '23 06:03

Hasnain Ali


1 Answers

Small change:

lister.insert(i*2, "Hello")
like image 134
Anmol Singh Jaggi Avatar answered Mar 15 '23 16:03

Anmol Singh Jaggi