Basic Operations in Lists

Insertion in Lists:

Inserting at the beginning of the array:

 To insert an element at the start of an Array, we'll need to shift all other elements in the array to the right by one index to create space for the new element. This is a very costly operation since each of the existing elements has to be shifted one step to the right. The need to shift everything implies that this is not a constant-time operation. The time taken for insertion at the beginning of an Array will be proportional to the length of the Array. In terms of time complexity analysis, this is a linear time complexity: O(N), where N is the length of the Array.

Inserting at any location in the array:

For inserting at any given index, we first need to shift all the elements from that index onwards one position to the right. Once space is created for the new element, we proceed with the insertion. If you think about it, insertion at the beginning is a special case of inserting an element at a given index—in that case, the given index was 0.

We can insert an element in any index position of the array using the 'insert' function in Python:

Syntax:

list_name.insert(index, element)

Now , let's insert "carrots"  at index position 2

grocery.insert(2, "carrots")

The below table shows how an element is inserted into a list:

apples
bananas
carrots
Cucumbers
dates
strawberries
oranges

Deletion in list:

Deletion in Array:

Deletion in an Array works in a very similar manner to insertion, and has the same three different cases:

  1. Deleting the last element of the Array.
  2. Deleting the first element of the Array.
  3. Deletion at any given index.

Deleting From the End of an Array:

Deletion at the end of an Array is similar to people standing in a line, also known as a queue. The person who most recently joined the queue (at the end) can leave at any time without disturbing the rest of the queue. Deleting from the end of an Array is the least time-consuming of the three cases. Recall that insertion at the end of an Array was also the least time-consuming case for insertion. pop() method in Python can be used to perform this operation.

pop() - This method deletes the element at the position mentioned in its arguments or deletes the last element if called without passing arguments.

After, grocery.pop() 

apples
bananas
carrots
dates
strawberries

After grocery.pop(1)

apples
carrots
dates
Strawberries

Deleting From the Start of an Array:

Next comes the costliest of all deletion operations for an Array—deleting the first element. If we want to delete the first element of the Array, that will create a vacant spot at the 0th index. To fill that spot, we will shift the element at index 1 one step to the left. Going by the ripple effect, every element all the way to the last one will be shifted one place to the left. This shift of elements takes O(N) time, where N is the number of elements in the Array.

We can use some python methods to delete elements from a list: 

remove() - This method deletes the first occurence of an element

Syntax

list_name.remove(element)

Lets see  how we can remove the element "cucumbers" frm the grocery list

grocery.remove("cucumbers")

This removes cucumbers from the list. Now the array becomes

apples
bananas
carrots
dates
Strawberries
oranges

Try here>>>