C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python List insert(i,x) MethodPython insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element. Signatureinsert(i, x) Parametersi : index at which element would be inserted. x : element to be inserted. ReturnIt does not return any value rather modifies the list. Let's see some examples of insert() method to understand it's functionality. Python List insert() Method Example 1Let's see an example to insert an element at 3 index of the list. # Python list insert() Method # Creating a list list = ['1','2','3'] for l in list: # Iterating list print(l) list.insert(3,4) print("After extending:") for l in list: # Iterating list print(l) Output: 1 2 3 After extending: 1 2 3 4 Python List insert() Method Example : listIt is possible to insert a list as an element to the list. See the example where a list is inserted at specified index. # Python list insert() Method # Creating a list list = ['1','2','3'] for l in list: # Iterating list print(l) list.insert(3,['4','5','6']) print("After inserting:") for l in list: # Iterating list print(l) Output: 1 2 3 After inserting: 1 2 3 ['4', '5', '6'] Python List insert() Method Example : TupleIt is possible to insert a tuple as an element to the list. See the example where a tuple is inserted at specified index. # Python list insert() Method # Creating a list list = ['1','2','3'] for l in list: # Iterating list print(l) list.insert(3,('4','5','6')) print("After inserting:") for l in list: # Iterating list print(l) Output: 1 2 3 After inserting: 1 2 3 ('4', '5', '6') Python List insert() Method Example : SetIt is possible to insert a set as an element to the list. See the example where a set is inserted at specified index. # Python list insert() Method # Creating a list list = ['1','2','3'] for l in list: # Iterating list print(l) list.insert(3,{'4','5','6'}) print("After inserting:") for l in list: # Iterating list print(l) Output: 1 2 3 After inserting: 1 2 3 {'4', '5', '6'}
Next TopicPython Lists
|