April 2017
Beginner to intermediate
312 pages
7h 23m
English
The insert() method enables adding an element at a specific position in the list. For example, let's consider the following example:
>>> day_of_week = ['Monday', 'Tuesday', 'Thursday', 'Friday', 'Saturday']
In the list, Wednesday is missing. It needs to be positioned between Tuesday and Thursday at position 2 (Python uses zero based indexing that is the positions/indexes of elements are counted as 0, 1, 2, and so on.). It could be added using insert as follows:
>>> day_of_week.insert(2, 'Wednesday') >>> day_of_week ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
Read now
Unlock full access