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'.
We can achieve that using the 'insert( )' Function.
x = ["Mohan", "Kriti", "Salim"] x.insert(1,"Nikhil") print(x)
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
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'.
And we get the below output,
Let us say, we have a List that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to insert a new name 'Mika' at the end of the List.
We can use the 'append( )' Function to achieve the above.
x = ["Mohan", "Kriti", "Salim"] x.append("Mika") print(x)
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Next, we have used the 'append( )' function to add the new name 'Mika' at the end of the List.
And we get the below output,