List Methods in Python


As we have learned in previous videos, if we want to store some data, python provides variables to do this. But, what if we want to store a set of data?

For this purpose, we will have to create more variables. But, creating so many variables is a tedious task to do. Also, we will have to choose so many names for these variables which will result in confusion for the programmer. To avoid this, we have the concept of a list in python. This is similar to the concepts of the array in C and C++.

In lists, the data are stored at memory locations, which can be accessed by the address, which resolves the problem of thinking and choosing too many names for different variables.

Now, we will learn about the lists in Python –

List In Python

  • A list is a collection of different values or different types of items.
  • Unlike an array in C/C++, it is capable of storing more than one type of value.
  • Items in the list are enclosed within the square brackets ( [] ) , and separated with the comma ( , ).

How to define a list in python?

 a = [“harry” , 1 , “johnny”, 9.5]

b = [1,  2 ,3 , 4 ,5]

When we create a list, the elements are indexed with a specific index which starts from 0 and continues till n-1. Where n denotes the total number of elements in the list. This type of indexing is called forward indexing.

Apart from this, there is one more way which is called backward indexing. In this, the last element is indexed as -1 and the second last as -2, and this way it continues backward till –n, where n denotes the number of elements.

Through the concept of indexing, we can jump to a specific element and update or modify the content at that index.

We will understand it better with the help of the example given below –

A = [“john” , 5 , “shyam” , 11.5]

These are indexed as such in the list –

If we want to modify the content, for example, we want the replace the value 1 stored in the index ‘1’ to 5, so we can do this with the help of its index.

If we write A[1] = 5

The current value at the index ‘1’ will be updated to ‘5’.

Now, we will learn about some useful keywords that are used in lists.

List Slicing in Python

We can use the slicing operator to access a range of list –

my_list = ['y','u','v','a','y','a','n','a']

# elements from index 2 to index 4


# elements from index 5 to end


# elements beginning to end


Output –

[‘v’, ‘a’, ‘y’]

[‘a’, ‘n’, ‘a’]

[‘y’, ‘u’, ‘v’, ‘a’, ‘y’, ‘a’, ‘n’, ‘a’]

Add/Change List Elements

We can change the values at any position in a List. That’s why, lists are said to be mutable.

For example –

odd = [2,4,6,8]

odd[0] = 1


Output –


Delete List Elements

Python Del statement can be used to delete one or more items from a list. It can even delete the list directly.

Example –

list1 = [‘h’, ‘a’, ‘r’, ‘s’, ‘h’, ‘i’, ‘t’]

del list1[2]


del my_list

 Output –

[‘h’, ‘a’, ‘s’, ‘h’, ‘i’, ‘t’]

Similarly, there are many more functions of the python list which are mentioned below with their uses.

Now, we will be discussing all of these list functions in detail –

Append() –

This method adds an element to the end of the existing array.

Syntax –


Example –

list1 = ['good', 'boy']


print list1

Output –

good boy studies

Extend() –

This method is used to add all the elements of one list to at the end of another list.

Syntax –


This will insert the second list at the end of first lise.

Example –

list_1 = [1,4,5,6,2]

list_2 = [11,12,15]


print('List after extend():', list_1)

Output –


Insert() –

This method is used to insert a value at a specific position in a list.

Syntax –


Example –

students = ['ram', 'shyam', 'rohit']

students.insert(1, "rahul")

Output –

[‘ram’, ‘rahul’, ‘shyam’, ‘rohit’]

Remove() –

This function removes a specific value from a list. In case a value occurs multiple times, it removes the first occurrence of that value.

Syntax –


Example –

list1= [2, 3, 5, 7, 9, 11]


print('Updated List: ',list1)

Output –

Updated List:  [2, 3, 5, 7, 11]

Pop() –

This function deletes and returns the last value of the value at specified index from the list

Syntax –


Here index is passed only if we want to delete a specific value

Example –

list1 = [1, 2, 3, 4, 5, 6]

print(list1.pop(), list1)

print(list1.pop(0), list1)

Output –

[1, 2, 3, 4, 5]

[2, 3, 4, 5]

Clear() –

This function removes all the elements from the list.

Syntax –


Example –

students = ['ram', 'shyam', 'rohit']


Output –


Index() –

This method searches the whole list and as soon as the value is found, it returns its index.

Syntax –

list_name.index(element, start, end)

In this, start, end is optional if we want to search between specific positions

Example –

list2 = ['harshit', 'is', 'a', 'good', 'boy']


Output –


Count() –

This function returns the number of elements with a specific value

Syntax –


Example –

students = ['ram', 'shyam', 'rohit', 'ram']

x = students.count("ram")


Output –


Sort() –

This function sorts a list in ascending order

Syntax –


Example –

numbers = [11, 3, 7, 5, 2]



Output –

[2, 3, 5, 7, 11]

Reverse() –

This method reverses the entire list, i.e., The first element becomes the last and the last becomes first.

Syntax –


Example –

list1 = [1, 2, 3, 4, 1, 2, 6]



Output –

[6, 2, 1, 4, 3, 2, 1]

Copy() –

This function returns a shallow copy of the list.

Syntax –

new_list = list.copy()

Example –

list = [2, 3, 5]

numbers = list.copy()

print('Copied List:', numbers)

In this article, we have learned about the list and some of the useful functions that are used to manipulate lists. These are used very Often by the programmers of use the list by modifying them

Related Quiz


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.