Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

manipulate list using slice assignment

# Use Slice Assignment 
l1 = [10,20,30,40,50,60,70,80,90]

#Replace
l1[1:3] = [21,31]
l1
#[10, 21, 31, 40, 50, 60, 70, 80, 90]
#Delete
l1[1:3] = []
l1
#[10, 40, 50, 60, 70, 80, 90]
#Add
l1[1:1] = [10,20,30]
l1
#[10, 10, 20, 30, 40, 50, 60, 70, 80, 90]
 
PREVIOUS NEXT
Tagged: #manipulate #list #slice #assignment
ADD COMMENT
Topic
Name
2+9 =