DEV Community

loading...

Some Important List method that all programmers must know

oladejo abdullahi
I am web developer and data scientist
・3 min read

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.

Letter=['a','b','c','d','e']
Letter.append('f')
print(Letter)
Enter fullscreen mode Exit fullscreen mode

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

Letter=['a','g','c','f','e','b']
Letter.sort()
print(Letter)
Enter fullscreen mode Exit fullscreen mode

Example2
the codes below sort out the number

numeric=[1,4,2,7,5,3,6]
numeric.sort()
print(numeric)
Enter fullscreen mode Exit fullscreen mode

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

myList=['a',3,'c',3,'e','b','a','b']
count_3=myList.count(3)
print(count_3)
Enter fullscreen mode Exit fullscreen mode

Example2
let's try to count the number of a's

myList=['a',3,'c',3,'e','b','a','b']
count_a=myList.count('a')
print(count_a)
Enter fullscreen mode Exit fullscreen mode

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

myList=['a',3,'c',3,'e','b','a','b']
index_a=index('a')
print(index_a)
Enter fullscreen mode Exit fullscreen mode

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

myList=['a',3,'c',3,'e','b','a','b']
index_k=myList.count('k')
print(index_k)
#this return error
Enter fullscreen mode Exit fullscreen mode

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

Letter=['a','g','c','f','e','b']
Letter.reverse()
print(Letter)
Enter fullscreen mode Exit fullscreen mode

Example2
the codes below reverse the number

numeric=[1,4,2,7,5,3,6]
numeric.reverse()
print(numeric)
Enter fullscreen mode Exit fullscreen mode

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

Letter=['a','g','c','a','e','b']
Letter.remove('a')
print(Letter)
Enter fullscreen mode Exit fullscreen mode

Example2
the codes below remove 4 from the list

numeric=[1,4,2,7,5,3,6]
numeric.remove(4)
print(numeric)
Enter fullscreen mode Exit fullscreen mode

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

Letter=['a','g','c','a','e','b']
Letter.pop(2)#because the index of 'c' is 2 
print(Letter)
Enter fullscreen mode Exit fullscreen mode

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.

Letter=['a','g','c','a','e','b']
Letter.insert(2,'x')# now the index of 'c' is 3 not 2
print(Letter)
Enter fullscreen mode Exit fullscreen mode

Now I believe you learn something. remember to like.

Discussion (0)

Forem Open with the Forem app