Lists
A list of 5 items has indexes 0 to 4, so menu[4] is the last item and menu[5] stops the program with IndexError.
One variable can hold a whole menu
A school lunch menu has several dishes, and a shopping list has several items. You could make one variable per dish, dish1, dish2, dish3, but that breaks the moment the menu grows. A list keeps any number of values in one variable, in order. Write the values between square brackets, separated by commas.
menu = ["rice", "seaweed soup", "bulgogi", "kimchi", "milk"]
print(menu)
print(len(menu))
print(menu[0], menu[-1])['rice', 'seaweed soup', 'bulgogi', 'kimchi', 'milk']
5
rice milkLists use the same index numbers as strings: 0 for the first item, -1 for the last, and slices such as menu[1:3]. len() gives the number of items. When you print a whole list, Python shows the brackets and puts each string in single quotes, so you can see exactly where each item starts and ends.
Unlike strings, lists can change
A string cannot be edited in place, but a list can. Assign to an index to replace an item. append() adds an item at the end, remove() deletes the first matching value, and pop() takes the last item off and gives it back to you. The keyword in checks whether a value is in the list. These methods change the list itself, which is the big difference from string methods.
cart = ["eggs", "milk"]
cart.append("tofu")
cart[0] = "ramen"
print(cart)
cart.remove("milk")
last = cart.pop()
print(cart, last)
print("tofu" in cart)['ramen', 'milk', 'tofu']
['ramen'] tofu
False| After this line | cart |
|---|---|
| cart = ["eggs", "milk"] | ['eggs', 'milk'] |
| cart.append("tofu") | ['eggs', 'milk', 'tofu'] |
| cart[0] = "ramen" | ['ramen', 'milk', 'tofu'] |
| cart.remove("milk") | ['ramen', 'tofu'] |
| last = cart.pop() | ['ramen'] (last is 'tofu') |
Lists of numbers come with ready-made tools
For a list of numbers, sum(), min() and max() do what their names say. sorted() gives back a new list in order and leaves the original alone, while the method sort() reorders the list itself and returns nothing. The average is the sum divided by the count, which is a nice use of len().
scores = [72, 95, 88, 64, 81]
print(sum(scores), min(scores), max(scores))
print(sum(scores) / len(scores))
print(sorted(scores))
print(scores)400 64 95
80.0
[64, 72, 81, 88, 95]
[72, 95, 88, 64, 81]Two names can point at the same list
Here is the part that catches almost everyone. After b = a, there are not two lists. There is one list with two names. Change it through b, and a sees the change too, because a and b are the same list. With numbers and strings you never notice this, since they cannot be changed in place. With lists you do. If you want a separate copy, ask for one with a.copy() or the slice a[:].
a = ["rice", "soup"]
b = a
b.append("kimchi")
print(a)
c = a.copy()
c.append("egg")
print(a)
print(c)['rice', 'soup', 'kimchi']
['rice', 'soup', 'kimchi']
['rice', 'soup', 'kimchi', 'egg']Lists and loops belong together. A list of 30 test scores is only useful if a program can visit every one of them without writing 30 separate lines, and that is what the for loop in the unit on loops does. Until then, sum(), min(), max() and len() do the visiting for you, one call each.
| list | Values in order between [ ], separated by commas |
| append(x) | Adds x to the end of the list |
| remove(x) | Deletes the first item equal to x |
| pop() | Takes the last item off and returns it |
| sorted(list) | New sorted list; the original is unchanged |
| list.sort() | Sorts the list itself and returns None |
| a.copy() | A separate copy of the list a |
- What does this print?
nums = [10, 20, 30, 40] print(nums[2]) - What does this print?
nums = [10, 20, 30, 40] print(nums[-1] + nums[0]) - What does this print?
bag = ["pen"] bag.append("ruler") bag.append("eraser") print(len(bag)) - What does this print?
x = [3, 1, 2] y = sorted(x) print(x, y)[3, 1, 2] [1, 2, 3][1, 2, 3] [1, 2, 3][1, 2, 3] [3, 1, 2][3, 1, 2] None
- What does this print?
x = [3, 1, 2] y = x.sort() print(x, y)[3, 1, 2] [1, 2, 3][1, 2, 3] [1, 2, 3][1, 2, 3] NoneAn error
- What does this print?
a = [1, 2] b = a b.append(3) print(a)[1, 2][1, 2, 3][3]An error
- What does this print?
marks = [70, 90, 80] print(sum(marks) / len(marks)) - What does this print?
stations = ["Seoul", "Yongsan", "Noryangjin", "Daebang"] print(stations[1:3]) - What does this print?
stack = ["a", "b", "c"] top = stack.pop() print(top, len(stack)) - What does this print?
print("milk" in ["eggs", "milk", "tofu"]) - Put the lines in order so the program prints the list below.Output
['homework', 'piano']
- "The last item is menu[len(menu)]." With 5 items the indexes stop at 4, so menu[5] stops with IndexError: list index out of range. Use menu[-1] or menu[len(menu) - 1].
- "menu = menu.append(x) keeps the new list." append() changes the list and returns None, so this line replaces the whole list with None. Write menu.append(x) on its own.
- "b = a gives me a backup of the list." It gives the same list a second name. Changes through either name show up in both. Use b = a.copy() for a real copy.
- A list holds values in order: [a, b, c].
- Indexes and slices work as for strings; the last index is len - 1.
- append, remove, pop and index assignment change the list itself.
- sorted() returns a new list; sort() changes the list and returns None.
- b = a shares one list; use a.copy() for a separate one.
Sources for this course (6)
- Python Software Foundation, The Python Tutorial for Python 3.12 (docs.python.org/3.12/tutorial: 2 Using the Python Interpreter, 3 An Informal Introduction to Python (numbers, text, lists), 4 More Control Flow Tools (if, for, range, break and continue, defining functions, default argument values, 4.10 coding style: 4-space indentation), 5 Data Structures (list methods, 5.5 dictionaries, insertion order), 8 Errors and Exceptions (syntax errors, exceptions, reading the traceback, handling exceptions), 15 Floating-Point Arithmetic (0.1 + 0.2))
- Built-in Functions (docs.python.org/3.12/library/functions.html: print, input, len, int, float, str, type, round (round half to even: round(0.5) and round(-0.5) are 0, round(1.5) is 2), range, sum, min, max, sorted)
- Built-in Types (str methods upper, lower, replace, strip; dict.get; list.append, pop, remove, sort returning None)
- General Python FAQ (docs.python.org/3.12/faq/general.html: Guido van Rossum began Python in the 1989 Christmas holidays, posted it to USENET in February 1991, named it after Monty Python's Flying Circus)
- Curriculum for the Korean pages: 교육부 고시 제2022-33호 [별책 10] 정보과 교육과정 (PDF read 2026-10-19: 중학교 정보 [9정03-05] 리스트 등 데이터를 순차적으로 저장하는 구조, [9정03-06] 논리 연산과 중첩 제어 구조, [9정03-07] 함수 활용과 디버거로 오류 수정, [9정03-08] 실생활 문제를 프로그래밍으로 해결, 적용 시 고려 사항 「학생의 수준을 고려하여 적합한 프로그래밍 언어를 선정」; 고등학교 정보 [12정03-04] 자료형, [12정03-05] 표준입출력, [12정03-07] 제어 구조)
- Bug story only: Smithsonian National Museum of American History, Log Book With Computer Bug, 1947, Harvard Mark II, "first actual case of bug being found", probably not Hopper's log book (americanhistory.si.edu/collections/object/nmah_334663, read 2026-10-19)
Checked: 2026-10-19