This is yet another cheatsheet I made for myself when studying for Leetcode and Hackerrank kinds of interviews. It’s organized in a way that makes sense to me.

I previously made a Ruby Arrays Cheatsheet you can check out.

All examples were tested using the Python 3.12.3 REPL.

Initializing a List

Empty List

1
my_list = []

List with Elements

Initializing a list of integers:

1
2
3
4
my_list = [1, 2, 3, 4, 5]

print(my_list)
# [1, 2, 3, 4, 5]

Initializing a list of strings:

1
2
my_list = ["A", "B", "C"]
# ['A', 'B', 'C']

You can mix different types of elements:

1
2
3
4
my_list = [1, "A", [2, 3], {"my_key": "my_value"}]

print(my_list)
# [1, 'A', [2, 3], {'my_key': 'my_value'}]

Adding Elements

At the Beginning of a List

We can also use insert() to add elements to the beginning of a list by specifying index 0:

1
2
3
4
5
my_list = [1, 2, 3]
my_list.insert(0, 4)

print(my_list)
# [4, 1, 2, 3]

At the End of a List

Use append() to add an element to the end of a list:

1
2
3
4
5
my_list = [1, 2, 3]
my_list.append(4)

print(my_list)
# [1, 2, 3, 4]

At a Specific Index

Use insert() and specify the index and element to add:

1
2
3
4
5
my_list = ["A", "B", "C"]
my_list.insert(1, "D")

print(my_list)
# ['A', 'D', 'B', 'C']

Removing Elements

At the Beginning of a List

We can use del() and specify index 0. This modifies the list in-place:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_list = [1, 2, 3, 4]
del(my_list[0])

print(my_list)
# [2, 3, 4]

del my_list[0]

print(my_list)
# [3, 4]

At the End of a List

We can use pop(). Modifies list in-place:

1
2
3
4
5
my_list = [1, 2, 3]
my_list.pop()

print(my_list)
# [1, 2]

We can also use del() and specify the last index, which should be len(list) - 1, but using pop() is a bit cleaner in my opinion.

At a Specific Index

Similar to removing an element from the beginning of the list, except we pass in a different index aside from 0:

1
2
3
4
5
my_list = ['A', 'B', 'C', 'D']
del(my_list[2])

print(my_list)
# ['A', 'B', 'D']

Retrieving Elements

The First Element

There’s no bult-in Python method to get the first element as far as I know, just specify index 0 like in most programming languages:

1
2
3
4
5
my_list = ['A', 'B', 'C', 'D']
char = my_list[0]

print(char)
# 'A'

The Last Element

Use index -1 to get the last element of a list. Negative indices start at the end of the list.

1
2
3
4
5
my_list = ['A', 'B', 'C', 'D']
char = my_list[-1]

print(char)
# 'D'

Element at a Specific Index

Similar to other programming languages, specify an index:

1
2
3
4
5
my_list = ['A', 'B', 'C', 'D']
char = my_list[2]

print(char)
# 'C'

Sorting Lists

We can use sort() to sort a list. This modifies the list in-place:

1
2
3
4
5
my_list = [4, 5, 1, 3, 2]
my_list.sort()

print(my_list)
# [1, 2, 3, 4, 5]

We can also use sorted(). This returns a new list and does not modify the original list:

1
2
3
4
5
6
7
8
my_list = [4, 5, 1, 3, 2]
sorted_list = sorted(my_list)

print(my_list)
# [4, 5, 1, 3, 2]

print(sorted_list)
# [1, 2, 3, 4, 5]

Looping Through Lists

Each Element

Use for <element> in <list_name> syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_list = ['A', 'B', 'C', 'D']

for char in my_list:
    print(char)

# output:
# A
# B
# C
# D

Each Index

Use range() and len() to loop through a list’s indices:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_list = ['A', 'B', 'C', 'D']

for index in range(len(my_list)):
    print(index)

# output:
# 0
# 1
# 2
# 3

Element and Index

Use enumerate() to loop through both elements and indices:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_list = ['A', 'B', 'C', 'D']

for index, element in enumerate(my_list):
    print(f'Index: {index}, Element: {element}')

# output:
# Index: 0, Element: A
# Index: 1, Element: B
# Index: 2, Element: C
# Index: 3, Element: D

Other Things to Know

Lists vs Arrays

Note that there are both Arrays and Lists in Python. Arrays are saved contiguously in memory and have fixed sizes so they are faster for reading but insertion and deletion costs are high. Arrays can only have elements of the same type.

Lists are more flexible. Lists can have elements of different types and do not have fixed sizes. The flexibility of Lists results in more memory being used by these data structures.

Here’s an example of an Array of integers being created in Python.

1
2
3
4
5
6
7
import array

# requires a more verbose syntax compared to a List
a = array.array('i', [1, 2, 3, 4, 5])

print(a)
# array('i', [1, 2, 3, 4, 5])

Attempting to add an element that doesn’t match the type of the existing elements results in an error:

1
2
3
4
5
6
7
8
import array

a = array.array('i', [1, 2, 3, 4, 5])
a.append("str")

# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: 'str' object cannot be interpreted as an integer

Appending elements of the same type will work as expected:

1
2
3
4
5
6
7
import array

a = array.array('i', [1, 2, 3, 4, 5])
a.append(6)

print(a)
# array('i', [1, 2, 3, 4, 5, 6])

You can learn more about Arrays in this GeeksForGeeks article.

Reversing a List

We can reverse a list using this syntax. This does not modify a list in-place, it returns a new list:

1
2
3
4
5
6
7
8
my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]

print(my_list)
# [1, 2, 3, 4, 5]

print(reversed_list)
# [5, 4, 3, 2, 1]

We can also use sort(reverse=True) to reverse a list. This modifies the list in-place:

1
2
3
4
5
my_list = [1, 2, 3, 4, 5]
my_list.sort(reverse=True)

print(my_list)
# [5, 4, 3, 2, 1]

There is also a reversed() function. This function returns an iterator which we can then convert back to a list:

1
2
3
4
5
my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))

print(reversed_list)
# [5, 4, 3, 2, 1]

Get Number of Elements in a List

Use len() to get the number of elements in a list.

1
2
3
4
my_list = ['a', 'b', 'c']

print(len(my_list))
# 3

Count Number of Occurrences

We can check for the number of occurrences of a value in a list using .count(). In the example below, we check to see how many times the number 1 appears in the list, which is 5 times.:

1
2
3
4
my_list [1, 2, 3, 4, 5, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1]

my_list.count(1)
# 5

References