Phase 2 — Collections
append, remove & pop
The three list methods you'll use constantly
append — add to the end
JavaScript:
const fruits = ['apple', 'banana'];
fruits.push('cherry');Python:
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # ["apple", "banana", "cherry"]append() is Python's push() — it always adds to the end of the list.
remove — delete by value
Python's remove() deletes the first matching value, not an index.
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits) # ["apple", "cherry"]JS doesn't have a direct "remove by value" method — you'd normally write
arr.filter(f => f !== 'banana') to get a new array without it.
Note: if the value doesn't exist, remove() raises an error, so make sure it's actually
in the list first (more on checking that with in later in this phase).
pop — remove and return
pop() removes an item and gives it back to you.
fruits = ["apple", "banana", "cherry"]
last = fruits.pop()
print(last) # "cherry"
print(fruits) # ["apple", "banana"]Just like JS's arr.pop(). But Python's pop() also accepts an index, which JS's
pop() doesn't support directly:
fruits = ["apple", "banana", "cherry"]
first = fruits.pop(0)
print(first) # "apple"
print(fruits) # ["banana", "cherry"]