List in Python

This tutorial will teach you about the Python List type and how to effectively manipulate list elements.

What is a List?

A list is an ordered grouping of items.

Python indicates a list with square brackets ([]). The following is an example of an empty list:

Python

A list typically contains one or more items. A comma is used to separate two items (,). As an example:

Python

Because a list typically has a large number of elements, it’s best to name it with plural nouns, such as numbers, colours, and shopping carts.

A list of six numbers is defined in the example below:

Python

If you print the list, you’ll notice that the square brackets are included. For instance:

Python

Output

[1, 3, 2, 7, 9, 4]

The following example demonstrates how to create a string list:

Python

Output

[‘red’, ‘green’, ‘blue’]

A list can be made up of other lists. A list of lists is defined in the following example:

Python

Output

[[0, 0], [100, 100], [200, 200]]

Accessing List Items in Python

Because a list is an ordered collection, its elements can be accessed using indexes like this:

Python

Zero-based indexes are used in lists. To put it another way, the first element has an index of 0, the second has an index of 1, and so on.

For instance, here’s how to get to the first entry of the numbers list:

Python

Output

1

The second member from the list will be returned by numbers[1]:

Python

Output

3

You can use the negative index to get elements from the beginning of the list.

The last element is returned by list[-1]. The second last entry is returned by list[-2], and so on. For instance:

Python

Output

4 9

Modifying elements in a list in Python

To update the value of an element, use the following syntax:

Python

The example below explains how to make the first member of the numbers list 10:

Python

Output

[10, 3, 2, 7, 9, 4]

The following example demonstrates how to multiply the second element by ten:

Python

Output

[1, 30, 2, 7, 9, 4]

The third element is then divided by two as follows:

Python

Output

[1, 3, 1.0, 7, 9, 4]

Adding elements to the list in Python

An element is appended to the end of a list using the append() function. For instance:

Python

Output

[1, 3, 2, 7, 9, 4, 100]

The insert() method inserts a new element into the list at any point.

For instance, the following code adds the number 100 to the second index of the numbers list:

Python

Output

[1, 3, 100, 2, 7, 9, 4]

Removing elements from a list in Python

You can use the del statement to remove an element from a list by specifying its position.

How to remove the first element from a list is demonstrated in the following example:

Python

Output

[3, 2, 7, 9, 4]

The pop() method retrieves the final entry of a list after removing it:

Python

Output

4
[1, 3, 2, 7, 9]

When you wish to remove an entry from a list while still having access to its value, you usually use the pop() method.

You use pop() with the element’s index to pop an element by its position. For instance:

Python

Output

3
[1, 2, 7, 9, 4]

To remove an element by value, you use the remove() method.

For example, the following removes the element with value 9 from the numbers list:

Python

Output

[1, 3, 2, 7, 4]

Leave Your Comment