Python List

List is one of the data types available in Python. A List is a member of data types called sequences. Various operations like indexing, slicing, multiplication, addition, etc., can be performed on these data types.
There are many benefits of using a List to store information, here are some important ones: –

  • Lists can be updated after being created meaning the items can either be changed, added or, removed.
  • Different data types can be used in Lists and this feature makes the List one of the most efficient and powerful data type in Python.
  • Elements in Lists can be duplicated. It is a property that is shared by only one other data type, i.e., a Tuple.
  • Lists stores data in an ordered way.

There are many factors that are considered when choosing a data type for various purposes like efficiency, security, etc. Those factors depend upon the properties of a data type.

Creating a List

A List is created by putting square brackets around all the elements that are needed to be included in the sequence. The elements are separated by commas.


Python creating list

The above example shows that a List has an ability to store two different types of data types.

Lists created by using multiple lists is called a multi – dimensional or a nested List. 


Python multi dimension nested list

Accessing Elements

Each member in a List has a unique index number can be accessed using that index number, this is called Indexing. The indexing in a List is similar to a String and thus starts from 0.


Error - Python accessing elements out of range

Elements in a Nested List can be accessed by nested indexing. This method can also be used to accessed sub-strings in a string.


Python nested indexing

The first index will access the members in the main string and the later indices will access the members present in the nested data type. If there are no nested data types in the nested member then Python interpreter will show an error.


Error - Python Error -No element

Negative Indexing

In Python, negative integers can also be done for indexing. To get a better understanding, take an example: –


Python negative indexing


There are many ways in Python that are used to print a whole List. To print a range of elements in a List, Slicing method is used. This is a type of operation and its syntax includes a colon “:”. By using this method, whole string can also be printed in the straight or reverse order.


Python slicing elements

Manipulation of Lists

As talked already, elements in a list can be updated or deleted. Also, elements can be added or removed from the list. Manipulation of List also includes the use of operators on them.

Updating a List

Adding Elements

The function or method “append()” can be used to add elements in a List. This method only a single element each time, however, for loop can be used to increase multiple elements.


Python manipulation updating list

This method only adds an element in the end of a List. To add elements at the desired location, the method insert() is used. It is used in this way: –


Python manipulation insert element

Updating Elements

Single elements can be updated simply by indexing. To update multiple elements in a List, slicing is done which is discussed later in this topic.


Python updating element

Deleting Elements

By using the “del” keyword, the items in a List can also be removed as per need. This can also remove a list entirely by using slicing.


Python deleting elements

#By default, the pop() function only deletes the last element in the List.


Python deleting elements pop method

Use of Operators

Operators can be used to perform various type of work on Lists. The ‘+’ operator can join two Lists together. As previously discussed, this is called concatenation.


Python plus join operator add

Also, the ‘*’ operator is used to repeat a List ‘n’ times.


Python repeat N times

List Functions

Some of the functions are already discussed above, here are some more useful functions for Lists.

List Functions
Function Usage
append() This function adds an element at the end of the List
copy() This function returns a copy of the List
count() It returns the number of elements in the List
extend() It adds any type of iterable to the end of a List
clear() Removes every element from the List
insert() It adds an element to a specific location
pop() Removes an element from the desired position
remove() Removes an element with the specified value
reverse() Reverses the List, i.e., positive index becomes negative and vice versa
sort() Sorts a List
insert() It adds an element to a specific location
index() Returns the index of the first element in the List with the value

Tutorials for all brains!