Python List

Facebooktwitterredditpinterestlinkedinmailby feather

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating a list in Python is simple as follows:

Indexing and Slicing in List

Indexing in List:

Indexing of list can be done by using [] operator.

list =[ ‘P’, ‘Y’ , ‘T’ , ‘H’ , ‘O’ ,N’]                                                                               

Python List

The values in the list can be accessed by its index position. The index starts from 0 to n-1. The first element is stored in the 0th index, and the second element is stored in the first index and so on.

The rightmost index position is -1, and its adjacent left index is -2 and so on. For example:

Python List 1

Slicing in List

Python provides a facility to get a sub-list of the list. Sometimes we require only some data from a given list then slicing makes it happen. The syntax is:

  • <strong>start</strong> refers to the starting position of the list element. 
  • <strong>stop</strong> refers to the index of the element where we should stop just before to finishing of our slice. 
  • <strong>step</strong> allows you to take each nth-element within a start:stop range.

Let’s look at this concept using below example:



In the above example, the start is equal to 2, so slicing starts from the second position. The value at index 2 is 30.

The stop indicates the last element of the list, and its value is 60. Hence it will stop just before to the stop index.

The steps permit to take each nth-element within a start: stop range.

Python List 2

We can skip the start index and then it will starts from 0 by default. For example:


So, list[:4] is equivalent to the list[0:4]. It is a shortcut to extract first nth element 

We can skip the stop index also. It will return last n elements of the given list. For example:


In the above example, we skip the stop index that’s mean it takes elements from the start position to end of the list.



In the above program, we can omit start and stop the argument and use only step argument. By providing step, we can skip some elements.

Accessing elements of list using negative indexing


Modification (update) in List

Lists are mutable; it means we can modify the elements of the list using the slice and assignment operator. The append() function of list is also used to add value in list.


Deletion in List

The del keyword is used to delete the element of list. It is used when we know exactly which element needs to be deleted. The remove () or pop () method can also be used for deleting the element. For example:


We can also delete the entire list. As per the following example:


Basic Operations in List

There are various operations in the list as follows.

  • Repetition

The repetition operator (*) is used to repeat list elements for multiple times.


  • Concatenation

The concatenation operator(+) is used to concatenate two or more list.


  • Iteration

The list can be iterated using for loop


  • Membership

If a particular item exists in a particular list than It returns true otherwise false.


Built-in functions in List

Sr. no. Functions Description
1. len(list) It is used to compute the length of list
2. cmp(list1, list2) It compares the elements of both the lists.
3. max(list) It returns the maximum element of the list.
4. min(list) It returns the minimum element of the list.
5. list(seq) It changes any sequence to the list.
Facebooktwitterredditpinterestlinkedinmailby feather