Python List insert() Method

Python 列表Python Lists


Description

The insert() function is used to insert the specified object at the specified position in the list.

Syntax

The syntax of the insert() method is:

list.insert(index, obj)

Parameters

  • index -- The index position where the object obj needs to be inserted.
  • obj -- The object to be inserted into the list.

Return Value

This method has no return value, but it inserts the object at the specified position in the list.

Example

The following example demonstrates the usage of the insert() function:

Example

#!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc'] aList.insert( 3, 2009) print "Final List : ", aList

The output of the above example is as follows:

Final List : [123, 'xyz', 'zara', 2009, 'abc']

Python 列表Python Lists

Other Extensions