Three list operations you'll reach for daily: add an item, count items, check if an item is there. What are the natural English words?
Append, length, contains?
Two of three. Python uses append and len (same as for strings yesterday). For "contains" the keyword is in — same little word that's part of for loops:
numbers = [1, 2, 3]
numbers.append(4) # adds 4 to the end → [1, 2, 3, 4]
print(len(numbers)) # 4
print(3 in numbers) # True
print(99 in numbers) # Falsenumbers.append(4) — that dot syntax is new. Why is append written with a dot but len is a function?
append is a method — a function that belongs to the list. The list "owns" it; you call it on a specific list. len is a free-standing function that works on any sized thing — strings, lists, dicts. Different style, same idea: pass the value, get a result.
And append modifies the list in place — it doesn't return a new list?
Common gotcha. numbers.append(4) returns None. The list object itself is changed. So result = numbers.append(4) puts None in result — almost certainly not what you wanted. Just call .append, don't try to capture its return.
append, len, inThree very common operations. The first uses method syntax (list.something()); the others are familiar — len() and in you've already seen.
.append(item) — add to the endnames = ["Ada"]
names.append("Bob")
names.append("Cleo")
print(names) # ['Ada', 'Bob', 'Cleo']append modifies the list in place. It returns None, not the new list — don't try to chain it or capture the return:
result = names.append("Dee")
print(result) # None — gotchalen(list) — count itemsSame len as for strings:
print(len([1, 2, 3])) # 3
print(len([])) # 0in — membership testfruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("durian" in fruits) # FalseReturns a boolean. Useful inside if:
if "apple" in fruits:
print("got an apple")And not in works as the negation:
if "durian" not in fruits:
print("no durian")| Style | Example | Where it lives |
|---|---|---|
| Function | len(numbers) | Free-standing — works on any sized thing |
| Method | numbers.append(4) | Attached to the list — only works on lists |
Methods are accessed with a dot: value.method(args). Most container types (lists, dicts, strings) ship with their own methods. You'll meet more as you go.
| Method | Effect |
|---|---|
.append(x) | add x to the end |
.pop() | remove and return the last item |
.remove(x) | remove the first occurrence of x (or ValueError) |
.sort() | sort the list in place |
.reverse() | reverse in place |
Not all needed today — keep them in mind as you write more code.
Three list operations you'll reach for daily: add an item, count items, check if an item is there. What are the natural English words?
Append, length, contains?
Two of three. Python uses append and len (same as for strings yesterday). For "contains" the keyword is in — same little word that's part of for loops:
numbers = [1, 2, 3]
numbers.append(4) # adds 4 to the end → [1, 2, 3, 4]
print(len(numbers)) # 4
print(3 in numbers) # True
print(99 in numbers) # Falsenumbers.append(4) — that dot syntax is new. Why is append written with a dot but len is a function?
append is a method — a function that belongs to the list. The list "owns" it; you call it on a specific list. len is a free-standing function that works on any sized thing — strings, lists, dicts. Different style, same idea: pass the value, get a result.
And append modifies the list in place — it doesn't return a new list?
Common gotcha. numbers.append(4) returns None. The list object itself is changed. So result = numbers.append(4) puts None in result — almost certainly not what you wanted. Just call .append, don't try to capture its return.
append, len, inThree very common operations. The first uses method syntax (list.something()); the others are familiar — len() and in you've already seen.
.append(item) — add to the endnames = ["Ada"]
names.append("Bob")
names.append("Cleo")
print(names) # ['Ada', 'Bob', 'Cleo']append modifies the list in place. It returns None, not the new list — don't try to chain it or capture the return:
result = names.append("Dee")
print(result) # None — gotchalen(list) — count itemsSame len as for strings:
print(len([1, 2, 3])) # 3
print(len([])) # 0in — membership testfruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("durian" in fruits) # FalseReturns a boolean. Useful inside if:
if "apple" in fruits:
print("got an apple")And not in works as the negation:
if "durian" not in fruits:
print("no durian")| Style | Example | Where it lives |
|---|---|---|
| Function | len(numbers) | Free-standing — works on any sized thing |
| Method | numbers.append(4) | Attached to the list — only works on lists |
Methods are accessed with a dot: value.method(args). Most container types (lists, dicts, strings) ship with their own methods. You'll meet more as you go.
| Method | Effect |
|---|---|
.append(x) | add x to the end |
.pop() | remove and return the last item |
.remove(x) | remove the first occurrence of x (or ValueError) |
.sort() | sort the list in place |
.reverse() | reverse in place |
Not all needed today — keep them in mind as you write more code.
Create a free account to get started. Paid plans unlock all tracks.