Python Lists

In Python, a list is an ordered collection of elements, which can be of any type, and is defined using square brackets [].

List data can be accessed in much the same way as strings.

colors = ['red', 'blue', 'green']
print(colors[0])    ## red
print(colors[2])    ## green
print(len(colors))  ## 3

FOR and IN

for is an easy way to look at each element in a list.

This for example takes each element in the list squares and adds them to get the sum value of 30.

squares = [1, 4, ,9, 16]
sum = 0
for num in squares:
    sum += num
print(sum)  ## 30

in is commonly used to check if an element exists in a list and return a boolean value.

This example checks for the existence of the value from if against the list and return a boolean value

list = ['bowie', 'bolan', 'ferry']
if 'bowie' in list:
    print('yes')

Range

The range(n) function yeilds numbers in the given range.

This example would generate 0 through 9

for i in range(10)
    print(i)

While loop

A while loop is a control flow statement that allows you to execute a block of code repeatedly while a certain condition is true.

Print an incremental number until you reach a target

count = 1
while count <= 5:
    print(count)
    count += 1

List methods

Common methods that can be used with lists in Python:

append(item) - adds an item to the end of the list

extend(iterable) - adds all the elements of an iterable to the end of the list

insert(index, item) - inserts an item at a specified index

remove(item) - removes the first occurrence of an item in the list

pop(index) - removes and returns the item at a specified index (or the last item if no index is specified)

index(item) - returns the index of the first occurrence of an item in the list

count(item) - returns the number of times an item appears in the list

sort() - sorts the list in ascending order

reverse() - reverses the order of the elements in the list

clear() - removes all the elements from the list.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]
my_list = [1, 2, 3]
my_list.insert(0, 0)
print(my_list)  # Output: [0, 1, 2, 3]
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list)  # Output: [1, 3, 2]

List Build Up

One common pattern is to start a list as the empty list [], then use append() or extend() to add elements to it:

Append example

my_list = []
my_list.append(1)
my_list.append(2)
my_list.append(3)
print(my_list)  # Output: [1, 2, 3]

Extend example

fruits = ['apple', 'banana', 'cherry']
more_fruits = ['mango', 'pineapple', 'watermelon']

# Using the extend() method to add all elements of another list to the end of the first list
fruits.extend(more_fruits)

print(fruits) # Output: ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'watermelon']

List Slices

You can use list slices to extract a subset of elements from a list. The syntax for a slice is list[start:stop:step], where start is the index of the first element to include, stop is the index of the first element to exclude, and step is the size of the step between elements.

Extracting a sublist from the beginning of a list:

my_list = [1, 2, 3, 4, 5]
sub_list = my_list[:3]
print(sub_list)  # Output: [1, 2, 3]
Last modified July 21, 2024: update (e2ae86c)