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:

empty_list = []

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

todo_list = ['Learn Python List','How to manage List elements']

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:

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

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

print(numbers)

Output

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

The following example demonstrates how to create a string list:

colors = ['red', 'green', 'blue']
print(colors)

Output

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

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

coordinates = [[0, 0], [100, 100], [200, 200]]
print(coordinates)

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:

list[index]

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:

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

print(numbers[0])

Output

1

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

numbers = [1, 3, 2, 7, 9, 4]
print(numbers[1])

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:

numbers = [1, 3, 2, 7, 9, 4]
print(numbers[-1])
print(numbers[-2])

Output

4 9

Modifying elements in a list in Python

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

list[index] = new_value

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

numbers = [1, 3, 2, 7, 9, 4]
numbers[0] = 10

print(numbers)

Output

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

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

numbers = [1, 3, 2, 7, 9, 4]
numbers[1] = numbers[1]*10

print(numbers)

Output

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

The third element is then divided by two as follows:

numbers = [1, 3, 2, 7, 9, 4]
numbers[2] /= 2

print(numbers)

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:

numbers = [1, 3, 2, 7, 9, 4]
numbers.append(100)

print(numbers)

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:

numbers = [1, 3, 2, 7, 9, 4]
numbers.insert(2, 100)

print(numbers)

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:

numbers = [1, 3, 2, 7, 9, 4]
del numbers[0]

print(numbers)

Output

[3, 2, 7, 9, 4]

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

numbers = [1, 3, 2, 7, 9, 4]
last = numbers.pop()

print(last)
print(numbers)

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:

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

second = numbers.pop(1)

print(second)
print(numbers)

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:

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

numbers.remove(9)
print(numbers)

Output

[1, 3, 2, 7, 4]