May 2019
Beginner
528 pages
29h 51m
English
Lists also have methods that add and remove elements. Consider the list color_names:
In [1]: color_names = ['orange', 'yellow', 'green']
Method insert adds a new item at a specified index. The following inserts 'red' at index 0:
In [2]: color_names.insert(0, 'red')In [3]: color_namesOut[3]: ['red', 'orange', 'yellow', 'green']
You can add a new item to the end of a list with method
append:
In [4]: color_names.append('blue')In [5]: color_namesOut[5]: ['red', 'orange', 'yellow', 'green', 'blue']
Use list method
extend to add all the elements of another sequence ...
Read now
Unlock full access