Lists and Slicing¶

Lists¶

Lists in Python can store a collection of different values in an accessible format. They are created with values separated by commas in brackets[].

In [3]:
# Here is a list!
basic_list = [1, 2, 3, 4]
basic_list
Out[3]:
[1, 2, 3, 4]

Lists can be made with the basic data types.

In [7]:
# A list of integers
int_list = [5, 3, 90, 1394]
# A list of floats
float_list = [4.0, 9.57, 67.3]
# A list of strings
string_list = ['Karen Carpenter', 'The Beatles', 'ABBA', 'Billy Joel']

A list can also hold multiple different data types within it.

In [36]:
# A list with mixed data types
mixed_list = [1972, 'Carpenters', 'A Song For You']

The values in the list can be accessed using indexing. Lists are 0-indexed, which means that the value, or element, at index 0 is the first value. The first value of a list can be accessed like list[0] and likewise for later values of the list.

In [6]:
print(mixed_list[0])
print(mixed_list[2])
1972
A Song For You

A list can also contain another list as one of the elements.

In [37]:
# A list of lists
list_in_list = [['cardinal', 'blue jay', 'goldfinch'], ['pothos', 'philodendron', 'monstera'], ['chanterelle', 'morel']]

Elements of lists within lists can be accessed like list[0][0]

In [38]:
print(list_in_list[0][1])
print(list_in_list[1][2])
blue jay
monstera

Strings can also be indexed, just like lists.

In [10]:
character = "Garfield"
character[4]
Out[10]:
'i'

Elements can be accessed using negative indexing, which allows you to access elements from the end of the list or string.

In [11]:
print(character[-1])
print(character[-3])
d
e

There are two ways to append to lists, or add elements to the end:

  1. list.append(...)
  2. list + [...]
In [5]:
# A list
comic_strips = ["Garfield", "Peanuts", "Calvin and Hobbes"]
# Appending to a list with append()
comic_strips.append("FoxTrot")
print(comic_strips)
# Appending to a list with + [...]
comic_strips = comic_strips + ["Mutts"]
print(comic_strips)
['Garfield', 'Peanuts', 'Calvin and Hobbes', 'FoxTrot']
['Garfield', 'Peanuts', 'Calvin and Hobbes', 'FoxTrot', 'Mutts']

You can add values to the beginning of the list with [...] + list.

In [6]:
# Add a value to the beginning of the list
comic_strips = ["Blondie"] + comic_strips
comic_strips
Out[6]:
['Blondie', 'Garfield', 'Peanuts', 'Calvin and Hobbes', 'FoxTrot', 'Mutts']

Values can also be inserted to any index within a list using insert(). This puts the new value at that index, then shifts the other elements to the right.

In [7]:
# Insert a value at index 3
comic_strips.insert(3, "The Wizard of Id")
comic_strips
Out[7]:
['Blondie',
 'Garfield',
 'Peanuts',
 'The Wizard of Id',
 'Calvin and Hobbes',
 'FoxTrot',
 'Mutts']

A list can be reversed with slicing, but it can also be reversed with reverse().

In [8]:
comic_strips.reverse()

There are various various helpful math functions for lists that are built into Python.

In [9]:
num_list = [1,2,3,4]
# Determines the length of a list
length = len(num_list)
print("Length:", length)
Length: 4
In [10]:
# Determines the maximum value of a list
max_num = max(num_list)
print("Maximum value:", max_num)
Maximum value: 4
In [11]:
# Determines the minimum value of a list
min_num = min(num_list)
print("Minimum value:", min_num)
Minimum value: 1

Slicing¶

Use a colon [x:y] to indicate "from x up to but not including y".

Use two colons [x:y:z] to indicate "from x up to but not including y, counting by z".

Omit the x and/or y values to indicate the beginning or end:

  • [:y] = "from the beginning up to but not including y"
  • [x:] = "from x to the end"
  • [:] = "the whole thing"
  • [::-1] = "the whole thing in reverse (counting by -1)"
In [39]:
# List for slicing
slice_list = ["Fantastic Mr. Fox", "The Grand Budapest Hotel", "Asteroid City", "Isle of Dogs", "The French Dispatch"]
In [ ]:
# Elements from the beginning to the third element
slice_list[0:3]
Out[ ]:
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
In [ ]:
# Elements from the beginning to the third element
slice_list[:3]
Out[ ]:
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
In [ ]:
# Elements from the second element to the last element
slice_list[1:]
Out[ ]:
['The Grand Budapest Hotel',
 'Asteroid City',
 'Isle of Dogs',
 'The French Dispatch']
In [ ]:
# All elements
slice_list[:]
Out[ ]:
['Fantastic Mr. Fox',
 'The Grand Budapest Hotel',
 'Asteroid City',
 'Isle of Dogs',
 'The French Dispatch']
In [ ]:
# Elements from the first element to the third from the last element
slice_list[:-2]
Out[ ]:
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
In [ ]:
# All elements in reverse
slice_list[::-1]
Out[ ]:
['The French Dispatch',
 'Isle of Dogs',
 'Asteroid City',
 'The Grand Budapest Hotel',
 'Fantastic Mr. Fox']
In [ ]:
# All elements, counting by 2
slice_list[::2]
Out[ ]:
['Fantastic Mr. Fox', 'Asteroid City', 'The French Dispatch']
In [ ]:
# All elements, counting by 2, in reverse
slice_list[::-2]
Out[ ]:
['The French Dispatch', 'Asteroid City', 'Fantastic Mr. Fox']