Inserting elements in the list using for-loop and expand the first five items

Issue

list_1 =[1,2,3,4,5,6,7,8,9,10]

how can i get the above list in the form of below list (repeatation needed for first 5 elements only)

list_1 =[1,1,2,2,3,3,4,4,5,5,6,7,8,9,10]

Solution

list_1 =[1,2,3,4,5,6,7,8,9,10]

for index in range(0, 10, 2):
    list_1.insert(index, list_1[index])

print(list_1)

Output:

[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10]

Answered By – BhusalC_Bipin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published