Inserting element at a specific position:

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']

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.41.187