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[].
# Here is a list!
basic_list = [1, 2, 3, 4]
basic_list
[1, 2, 3, 4]
Lists can be made with the basic data types.
# 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.
# 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.
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.
# 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]
print(list_in_list[0][1])
print(list_in_list[1][2])
blue jay monstera
Strings can also be indexed, just like lists.
character = "Garfield"
character[4]
'i'
Elements can be accessed using negative indexing, which allows you to access elements from the end of the list or string.
print(character[-1])
print(character[-3])
d e
There are two ways to append to lists, or add elements to the end:
list.append(...)list + [...]
# 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.
# Add a value to the beginning of the list
comic_strips = ["Blondie"] + comic_strips
comic_strips
['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.
# Insert a value at index 3
comic_strips.insert(3, "The Wizard of Id")
comic_strips
['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().
comic_strips.reverse()
There are various various helpful math functions for lists that are built into Python.
num_list = [1,2,3,4]
# Determines the length of a list
length = len(num_list)
print("Length:", length)
Length: 4
# Determines the maximum value of a list
max_num = max(num_list)
print("Maximum value:", max_num)
Maximum value: 4
# 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)"
# List for slicing
slice_list = ["Fantastic Mr. Fox", "The Grand Budapest Hotel", "Asteroid City", "Isle of Dogs", "The French Dispatch"]
# Elements from the beginning to the third element
slice_list[0:3]
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
# Elements from the beginning to the third element
slice_list[:3]
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
# Elements from the second element to the last element
slice_list[1:]
['The Grand Budapest Hotel', 'Asteroid City', 'Isle of Dogs', 'The French Dispatch']
# All elements
slice_list[:]
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City', 'Isle of Dogs', 'The French Dispatch']
# Elements from the first element to the third from the last element
slice_list[:-2]
['Fantastic Mr. Fox', 'The Grand Budapest Hotel', 'Asteroid City']
# All elements in reverse
slice_list[::-1]
['The French Dispatch', 'Isle of Dogs', 'Asteroid City', 'The Grand Budapest Hotel', 'Fantastic Mr. Fox']
# All elements, counting by 2
slice_list[::2]
['Fantastic Mr. Fox', 'Asteroid City', 'The French Dispatch']
# All elements, counting by 2, in reverse
slice_list[::-2]
['The French Dispatch', 'Asteroid City', 'Fantastic Mr. Fox']