Lists

Create an empty list

L = [ ]
print(L)
[]
L = list()
print(L)
[]

Create a list with certain elements

L = [1, 2, 3]
print(L)
words = ['good', 'better', 'best']
print(words)
[1, 2, 3]
['good', 'better', 'best']

Append an element to a list

This modifies the list in-place using a list method called append.

L = [1, 2, 3]
L.append(4)
print(L)
[1, 2, 3, 4]

This creates a new list object using list concatenation and puts it back in L

L = [1, 2, 3]
L = L + [4]
print(L)
[1, 2, 3, 4]

Insert an element at the beginning of a list

This modifies the list in-place using the insert method.

L = ['better', 'best']
L.insert(0, 'good')
print(L)
['good', 'better', 'best']

This creates a new list using list concatenation and puts it back in L.

L = ['better', 'best']
L = ['good'] + L
print(L)
['good', 'better', 'best']

Iterate through a list

This is the recommended method.

L = [1, 2, 3, 4]
for x in L:
    print(x)
1
2
3
4

This is to be avoided whenever possible.

L = [1, 2, 3, 4]
for i in range(len(L)):
    print(L[i])
1
2
3
4

Iterate through a list with access to both index and element

This is recommended.

L = [10, 20, 30, 40]
for index, x in enumerate(L):
    print(index, x)
0 10
1 20
2 30
3 40
L = [10, 20, 30, 40]
for i in range(len(L)):
    print(i, L[i])
0 10
1 20
2 30
3 40

Find length of a list

L = [1.0, 3.5, 10.9, 5.3, -10.3]
print(len(L))
5

Determine presence of element in a list

L = ['good', 'random', 'better', 'best']
value = 'random'
print(value in L)
True

Remove an element from a list

L.remove(value) removes the leftmost occurrence of value from the list L if it exists. This operation is in-place. It throws an error if value is not present in L. Here is a graceful way of removing an element.

L = ['good', 'random', 'better', 'best']
value = 'random'
if value in L:
    L.remove(value)
print(L)
['good', 'better', 'best']

Remove all occurrences of element from a list

L = [10, 5, 3, 4, 10, 6, 9, 10]
value = 10
while value in L:
    L.remove(value)
print(L)
[5, 3, 4, 6, 9]

The reason we use L.copy() is to make sure that we don’t modify the list while we are iterating over it.

L = [10, 5, 3, 4, 10, 6, 9, 10]
value = 10
for x in L.copy():
    if x == value:
        L.remove(value)
print(L)
[5, 3, 4, 6, 9]

Find index of an element in a list

L.index(value) returns the index of the leftmost occurrence of value if it is present in the list. It will throw an error if value is not present in L. Here is a graceful way to express this:

L = [10, 20, 30, 40, 50]
value = 30
if value in L:
    index = L.index(value)
    print(index)
2

Sort a list

L.sort() sorts the list L in place. If you do not want to modify L, refer to method-2.

L = [3, 5, 1, 2, 5, 4, 6, 7, 8, 9]
L.sort()
print(L)
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9]
L = [3, 5, 1, 2, 5, 4, 6, 7, 8, 9]
out = sorted(L)
print(out)
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9]

Iterate through the list in reverse direction

This is recommended.

L = [1, 2, 3, 4, 5]
for x in reversed(L):
    print(x)
5
4
3
2
1
L = [1, 2, 3, 4, 5]
for i in range(len(L) - 1, -1, -1):
    print(L[i])
5
4
3
2
1

Copy a list

First, we will look at the wrong way of doing this:

P = [1, 2, 3, 4]
Q = P
P[-1] = 5
print(P)
print(Q)
[1, 2, 3, 5]
[1, 2, 3, 5]

Refer to this FAQ to understand why this happens. Now, the correct way.

P = [1, 2, 3, 4]
Q = P.copy()
P[-1] = 5
print(P)
print(Q)
[1, 2, 3, 5]
[1, 2, 3, 4]

Find dimension of a matrix

M = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12]]
m, n = len(M), len(M[0])
print(m, n)
3 4

Create a zero matrix

This snippet creates a \(3 \times 5\) zero matrix.

m, n = 3, 5
M = [ ]
for i in range(m):
    row = [ ]
    for j in range(n):
        row.append(0)
    M.append(row)
print(M)
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]