A list is python's slice. It's a resizable array. Unlike most languages,
elements do not need to all share the same type.
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
length
The len() builtin returns the length of a list.
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
checking that a list is not empty
An empty list returns false in an if statement.
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(requested_topping)
accessing the last element
As a short-hand, rather than getting the length of the list, you can index
using negative numbers. Using -1 will always return the last element of the
list. However, an empty list will still print an error.
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1])
looping
Remember the colon:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
numbers
The builtin range() function generates a list of numbers. Thus, creating
a "traditional" for loop is as follows:
for value in range(1, 5):
print(value) # 1 2 3 4
Note that the range is inclusive - exclusive. So 5 was not printed. The range function can accept other arguments if you need to loop over just even numbers or something.
In addition to range() there's also enumerate() if you need to index value:
animals = ["dog", "cat", "mouse"]
for i, value in enumerate(animals):
print(i, value)
numbered list creation
You can also use the range syntax to create a list manually:
squares = [value**2 for value in range(1, 11)]
while loops
You can also make a while loop and break and continue do what you'd expect.
i = 1
while i <= 5:
print(i)
i += 1
in operator
There's a short-hand for checking if a value is in a list:
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushrooms' in requested_toppings:
print('yay mushrooms!')
To invert this use not in.
adding elements
Append to the end of a list.
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
Insert at the start of the list (shifting other elements).
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
removing elements from a list
Using the del statement to remove at a certain index.
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
pop
motorcycles = ['honda', 'yamaha', 'suzuki']
last = motorcycles.pop()
print(last) # suzuki
print(motorcycles) # honda yamaha
by value
The remove methods deletes only the first occurrence of the value.
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.remove('honda')
copying
The slicing syntax is used to copy a list:
foods = ['pizza', 'falafel', 'carrot cake']
foods_copy = foods[:]
sorting
The sort() method is a quick way to sort a list (of strings) alphabetically.
The method takes various arguments such as reverse=True. This modifies
the list rather than making a copy.
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
The builtin function sorted() returns a copy of the list, having been sorted,
without changing the original list.
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(sorted(cars))
min and max
The builtin functions min() and max() operate on a list of numbers;
returning the minimum or maximum value.
slicing
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) // Prints elements 0, 1, and 2.