Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




insert( ) FUNCTION


The 'insert( )' Function is used to insert new values at any specific position in the List.


Let us say, we have a List that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to insert a new name 'Nikhil' in between 'Mohan' and 'Kriti'.


Example :


x = ["Mohan", "Kriti", "Salim"]
x.insert(1,"Nikhil")
print(x) 


Output :



  ['Mohan', 'Nikhil', 'Kriti', 'Salim']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Now, if we see the above diagram, 'Kriti' resides at position/index '1'. So, what we do is,just insert the new name 'Nikhil' at the position/index '1' using the 'insert( )' Function.


And 'Kriti' gets shifted to index/position '2'.


x.insert(1,"Nikhil")

java_Collections

And we get the below output,


['Mohan', 'Nikhil', 'Kriti', 'Salim']