# Python List Operations

Python is very famous among the professionals due to its simplicity and wide range support of libraries/functionalities available.

When we have a list, we may need to perform a lot of operations on it. Presenting some of the useful operations which are very much required in day to day life.

Consider we have a list something like below, mentioned the list indexes as well for reference. We will go through with some of the list based operations one by one:

1) Sorting a list:

sort() function sorts the list in ascending order. If you want to sort the list in descending order, then you need to set the value of reverse parameter to True.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.sort()       # sort function sorts the list`
`print(lst)       `

Output

`[1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 7, 7, 8]`

`x.sort(reverse = True) # sorts in descending order`

Point to Remember: You do not need to assign it to some other variable after sorting. It is in-place sorting by-default.

2) Delete Duplicate elements from list:

Python set always holds the unique values in it, that’s why it’s a very good hack of solving this problem.

First we convert our list to a set and later we convert the set back to the list.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst = list( set( lst ) )`
`print(lst)`

Output:

`[1, 2, 3, 4, 5, 6, 7, 8]`

.

3) Reverse a list:

Reverse function simply reverses the entire list.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.reverse()`
`print(lst) `

Output:

`[2, 2, 1, 4, 3, 3, 7, 4, 7, 2, 3, 8, 5, 1, 2, 1, 3, 6]`

4) List Slicing:

Sometimes, we may want to use/print only a subset of the list, but not the complete list. Taking a subset from a list is called list slicing.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`print(lst[:5])    # [6, 3, 1, 2, 1]`
`print(lst[10:])  # [4, 7, 3, 3, 4, 1, 2, 2]`
`print(lst[2:3])  # [1]`

[ : 5] is equivalent to [0 : 5] means we want all the elements from the starting (index no. 0) till 5th element (index no. 4)

[10 : ] means we want all the elements from the 11th element (index no. 10) till the last element of list

5) Adding a new element to list:

A new element can be added to the end of the list by calling append() function.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.append(10)`
`print(lst) `

Output:

`[6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2, 10]`

6) Insert element at specific index in list:

List provides an insert() function where the first parameter is the index number and second parameter is the value which you want to insert.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.insert(1, 10)  # insert 10 at index no. 1`
`print(lst)`

Output:

`[6, 10, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`

7) Remove last element from list:

pop() function deletes the last element from the list if no parameter is passed.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.pop()`
`print(lst) `

Output:

`[6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2]`

8) Remove element at specific index from list:

pop() function takes the parameter as well. Parameter value is the index number from which you want to delete the element.

Code:

`lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`
`lst.pop(1) # delete element from index 1`
`print(lst) `

Output:

`[6, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]`

9) Delete list:

Once your task is done, you can delete the list which will clear the allocated space from RAM. del() function deletes the list

Code:

`del(lst) `
`print(lst)  # since lst is deleted by previous statement, hence it doesn’t exist now`

Output:

`---------------------------------------------------------------------------`
`NameError                                 Traceback (most recent call last)`
`<ipython-input-40-b5cada25ed2a> in <module>`
`----> 1 lst`

`NameError: name 'lst' is not defined`

Bengaluru, India