Lists
A list holds many values in order. You write one with square brackets and commas.
items = ["apple", "bread", "milk"]
print(items)
print(len(items))
A list can hold numbers, strings, booleans, even other lists. Usually all the values in a list are the same kind of thing, but Python does not insist.
prices = [40, 25, 2.5]
mixed = ["bread", 25, True]
empty = []
print(prices)
print(mixed)
print(len(empty))
Indexing and slicing work exactly like strings
Positions start at 0, negatives count from the end, and slices use
[start:stop] with the stop left out.
items = ["apple", "bread", "milk", "eggs", "flour"]
print(items[0])
print(items[-1])
print(items[1:3])
print(items[:2])
print(items[2:])
A slice of a list is a new, smaller list. All the string tricks carry over:
items[:-1] is everything but the last, items[::2] is every second item,
and items[::-1] is the list reversed.
items = ["apple", "bread", "milk", "eggs", "flour"]
print(items[:-1])
print(items[::2])
print(items[::-1])
Is it in there?
in checks whether a value is one of the items.
items = ["apple", "bread", "milk"]
print("milk" in items)
print("eggs" in items)
Lists can change
Unlike strings, lists can be changed after you make them, without making a
new list. append adds an item at the end. Assigning to a position replaces
what is there.
items = ["apple", "bread"]
items.append("milk")
print(items)
items[0] = "pear"
print(items)
append returns nothing, which Python shows as None; it changes the list it was called on. A common
mistake is items = items.append("milk"), which sets items to None.
Try it
Last item
Write last_item(items) that returns the last value in the list items.
The list always has at least one item.
last_item(["apple", "bread", "milk"]) -> "milk"
last_item([7]) -> 7
def last_item(items):
...
Add to the list
Write add_item(items, item) that adds item to the end of the list
items and returns it. Change the list you are given rather than building
a new one; the checks look at the original list afterwards.
add_item(["apple"], "bread") -> ["apple", "bread"]
add_item([], "milk") -> ["milk"]
def add_item(items, item):
...
Is it on the list?
Write on_list(items, item) that returns True if item is one of the
values in items, and False otherwise.
on_list(["apple", "bread"], "bread") -> True
on_list(["apple", "bread"], "milk") -> False
def on_list(items, item):
...
Next: For loops.