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.12 REPL.

Initializing a List

Empty List

Python
my_list = []

List with Elements

Initializing a list of integers:

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

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

Initializing a list of strings:

Python
my_list = ["A", "B", "C"]
# ['A', 'B', 'C']

You can mix different types of elements:

Python
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:

Python
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:

Python
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:

Python
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:

Python
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:

Python
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:

Python
my_list = ['A', 'B', 'C', 'D']
del(my_list[2])

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

Retrieving Elements

The First Element

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

Python
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.

Python
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:

Python
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:

Python
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:

Python
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:

Python
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:

Python
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:

Python
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

There are both Arrays and Lists in Python. Arrays are saved contiguously in memory 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. 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.

Python
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:

Python
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:

Python
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:

Python
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 list.reverse() to reverse a list. This modifies the list in-place:

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

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:

Python
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.

Python
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:

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

my_list.count(1)
# 5

References