How to add a range of elements of a list to another list in Python

2 Answers

0 votes
source = [10, 20, 30, 40, 50, 60, 70]
target = [1, 2, 3]

# Add elements from index 2 to 5 (30, 40, 50)
target.extend(source[2:5])

print(target)  



'''
run:

[1, 2, 3, 30, 40, 50]

'''

 



answered Oct 16 by avibootz
0 votes
source = [10, 20, 30, 40, 50, 60, 70]
target = [1, 2, 3]

# Add elements from index 2 to 5 (30, 40, 50)
target = target + source[2:5]

print(target)  



'''
run:

[1, 2, 3, 30, 40, 50]

'''

 



answered Oct 16 by avibootz
...