In this passage I’ll show the notes of Containers in CS_61A.

Box-and-Pointer Notation

This is just an easy diagrams to show lists in Environment Diagrams like this
pVp1Jun.png

Processing Container Values

Several built-in functions take iterable arguments and aggregate them into a value.

sum

  • sum(iterable) -> value
  • sum(iterable, start) -> value

This expression return the sum of an iterable of numbers (Not strings) plus the value of parameter ‘start’ (which defaults to 0). When the iterable is empty, return start.

examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> sum([2,3,4])
9
>>> from operator import sum
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
from operator import sum
ImportError: cannot import name 'sum' from 'operator' (/usr/lib/python3.13/operator.py). Did you mean: 'sub'?
>>> sum([1, 2, 3])
6
>>> sum([1, 2, 3], 1)
7
>>> sum([1, 2, 3], [1, 2, 3])
Traceback (most recent call last):
File "<python-input-5>", line 1, in <module>
sum([1, 2, 3], [1, 2, 3])
~~~^^^^^^^^^^^^^^^^^^^^^^
TypeError: can only concatenate list (not "int") to list
>>> sum([[1, 2, 3], [4]], [5])
[5, 1, 2, 3, 4]

Max

  • max(iterable) -> value
  • max(iterable, key=func) -> value
  • max(a, b, c, …) -> value
  • max(a, b, c,…, key=func) -> value

With a single iterable argument , return its largest item.
with two or more arguments, return the largest argument.

example:

1
2
3
4
5
6
>>> max(1, 2, 3, 4)
4
>>> max([1, 2, 3, 4])
4
>>> max([1, 2, 3, 4], key=lambda x: -x)
1

All

  • all(iterable) -> bool

Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.