Typical operations on lists are the insertion and deletion of elements and list concatenation. With the slicing notation, list insertion and deletion become obvious; deletion is just replacing a part of a list with an empty list []:
L = ['a', 1, 2, 3, 4] L[2:3] = [] # ['a', 1, 3, 4] L[3:] = [] # ['a', 1, 3]
Insertion means replacing an empty slice with the list to be inserted:
L[1:1] = [1000, 2000] # ['a', 1000, 2000, 1, 3]
Two lists are concatenated by the plus operator + :
L = [1, -17] M = [-23.5, 18.3, 5.0] L + M # gives [1, -17, 23.5, 18.3, 5.0]
Concatenating a list n times with itself motivates the use of the multiplication operator *:
n = 3 n * [1.,17,3] # gives [1., 17, 3, 1., 17, 3, 1., 17, 3] [0] * 5 # gives [0,0,0,0,0]
There are no arithmetic operations on a list, such as elementwise summation or division. For such operations, we use arrays; see Section 3.2: A quick glance...