Some Important List method that all programmers must know
Tags: python, devp, tutorial
Published: 2020-12-13 12:00:27

Some important List method
Here are some list methods that every programmers must know
##`append(element)` 
this is used to add element to the end of the list. description formular for string is 
List.append(element)
Example
let say we have a list of 5 letters if we wish to add more letter to it we used append() method like the follwing.
```python
Letter=['a','b','c','d','e']
Letter.append('f')
print(Letter)
```
Notice: we don't need to assign any variable again for the addition immediately we used append it has been added.

##`sort()`
 sorts the list i.e it arrange the list in ascending order either numerically or alphabetically.
Example1
the codes below sort out the Letter
```python
Letter=['a','g','c','f','e','b']
Letter.sort()
print(Letter)
```
Example2
the codes below sort out the number
```python
numeric=[1,4,2,7,5,3,6]
numeric.sort()
print(numeric)
```
##`count(element)`
this is used to count the number of times element occurs in the list.
Example1
Let's try to count number of 3
```python
myList=['a',3,'c',3,'e','b','a','b']
count_3=myList.count(3)
print(count_3)
```
Example2
let's try to count the number of a's
```python
myList=['a',3,'c',3,'e','b','a','b']
count_a=myList.count('a')
print(count_a)
```
Note: you have to assign another variable to get the number of element. Also note different between string and number.

##`index(x)`
 returns the location of the first occurrence of x
Example
```python
myList=['a',3,'c',3,'e','b','a','b']
index_a=index('a')
print(index_a)
```
Note: if you don't have the element inside the element the python will return error.Also remember our numbering always start from '0'.
Example2
```python
myList=['a',3,'c',3,'e','b','a','b']
index_k=myList.count('k')
print(index_k)
#this return error
```
##`reverse()` 
reverse the list i.e it arrange the list in descending order either numerically or alphabetically. it is the opposite of sort().
Example1
the codes below reverse the Letter
```python
Letter=['a','g','c','f','e','b']
Letter.reverse()
print(Letter)
```
Example2
the codes below reverse the number
```python
numeric=[1,4,2,7,5,3,6]
numeric.reverse()
print(numeric)
```
##`remove(element)` 
this is used to remove or delete the first occurrence of element from the list.
Example
the codes below remove the first 'a' from the list
```python
Letter=['a','g','c','a','e','b']
Letter.remove('a')
print(Letter)
```
 Example2
the codes below remove 4 from the list 
```python
numeric=[1,4,2,7,5,3,6]
numeric.remove(4)
print(numeric)
```
##`pop(P)` 
removes the item at index p and returns its value. i.e it is very similar to remove but the different is that the index of the element we want to remove will be written not the element.
Example
let's try to remove 'c' from the list
```python
Letter=['a','g','c','a','e','b']
Letter.pop(2)#because the index of 'c' is 2 
print(Letter)
```
##`insert(p,x)`
this inserts x at index p of the list
i.e it work very it is used to update element to the list wherever we wish without deleting any element.
Example
let us try to put "x" where 'c' so that the index of c will shift to 3.
```python
Letter=['a','g','c','a','e','b']
Letter.insert(2,'x')# now the index of 'c' is 3 not 2
print(Letter)
```
Now I believe you learn something. remember to like.