In this article, we will understand lists in python along with create, access, update and delete operations on list. Moreover, we will go through all built-in functions and methods of list with examples.

A list is an inbuilt datatype available in Python which can store a sequence of elements. An elements of list are comma-separated values inside square bracket.  One of the important characteristic of list is that all elements of the list need not be of the same data type.

Example:

Like array and string, indices of the list also starts from 0 to n-1, where n is the total number of elements in the list. List can also be concatenated and sliced by indices.

Accessing & Updating Lists in Python

We can access the elements of the list by using the index or indices inside the square brackets.

Example:-

Click here to Copy the Code

The above code gives output:

Click here to Copy the Code

We can also create a list inside a list which makes list heterogeneous data structures.

Syntax : [list1[list2]]

Example:-

The above code gives output:

As list is a mutable data type in python, we can update an elements of list by providing the slice on the left hand side of the assignment operator. Moreover, we can also add a new element in a list by append() inbuilt method of list.

Click here to Copy the Code

The above code gives output:

Click here to Copy the Code

Deleting an Element from the Lists in Python

We can delete an element from the specific index of list by using del statement. Syntax : del list_name[index]

If we want to remove specific value from the list then we can use remove() inbuilt method of list. Syntax : list_name.remove(value)

Click here to Copy the Code

The above code gives output:

Click here to Copy the Code

Just like strings, lists also have some basic operators which eases the work done to carry out some basic operations and reduces the lines of code.

Basic Operators in List

Python ExpressionResults Description
len([1, 2, 3])3Length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Concatenation
[‘Hi!’] * 4[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]Repetition
3 in [1, 2, 3]TrueMembership
for i in [10, 20, 30]: print i,10 20 30Iteration

Unlike strings, list does have some built-in functions apart from methods. There are five such built-in functions.

Built-in functions of List

Built-in List FunctionsDescription
cmp(list1, list2)Compares elements of both lists.
len(list)Gives the total length of the list.
max(list)Returns element with max value from list.
min(list)Returns element with min value from list.
list(seq)Converts a tuple into list.

Let us see an example program which will contain important functions the above listed in it.

Click here to Copy the Code

The above code gives output:

Click here to Copy the Code

We will now see some important built-in methods of list:

Built-in Methods of List

Built-in List MethodsDescription
list.append(obj)It appends object “obj” at the end of list
list.count(obj)It returns count of occurrence of “obj” in list
list.extend(seq)It appends the contents of “seq” at the end of list
list.index(obj)It returns the lowest index of “obj” in the list
list.insert(index, obj)It inserts object “obj” at offset index of list
list.pop(obj=list[-1])It removes and returns last object or obj from list
list.remove(obj)It removes object “obj” from the list
list.reverse()It reverses the list
list.sort([func])It sorts objects of the list, if given then use compare func
Click here to Copy the Code

The above code gives output:

Click here to Copy the Code

The above example covers mainly all the methods of the list and is very much to understand. So that was all about lists in python. Although, we will still discuss about its mutability and also compare it with other sequences of python like strings, tuples and dictionaries in a post later on. It is very much necessary for you to understand all the sequences before having discussion on the mutability.

In next post, we will see one more sequence of python i.e. tuple.

Click here to know about Mutability in various sequences of Python.