> For the complete documentation index, see [llms.txt](https://calnotes.gitbook.io/cs61a-guidebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnotes.gitbook.io/cs61a-guidebook/structures-of-data/iterables/iterable-functions.md).

# Iterable Functions

Now that we've developed some familiarity with lists, let's quickly discuss some functions that can be used with them to change them or to retrieve values from lists!&#x20;

### Append

To add an element to the list, we use the append keyword. **It is essential to remember that append is a mutating function — it returns None. Don't use its return value for anything!**

```
>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
```

What happens when you try to append a list to a list?

```
>>> lst = [1, 2, 3]
>>> lst.append([4, 5])
>>> lst
[1, 2, 3, [4, 5]]
```

As we hopefully expected, `[4, 5]` is considered one element and appended to the list as such!&#x20;

### Extend

Contrary to `append`, extend accepts an iterable and appends all the elements of that iterable to the list. Extend is also a mutating function and returns None.

```
>>> lst = [1, 2, 3]
>>> lst.extend([4, 5])
>>> lst 
[1, 2, 3, 4, 5]
```

What happens when we try to extend the list with something that is not an iterable?

```
>>> lst = [1, 2, 3]
>>> lst.extend(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
```

What happens when we try to extend the list with a list that contains more lists?

```
>>> lst = [1, 2, 3]
>>> lst.extend([4, [5, 6]])
>>> lst
[1, 2, 3, 4, [5, 6]]
```

Did you figure out that one? It's absolutely alright if you didn't! Hopefully this example helped you develop the intuition behind how `extend` appends every element of the iterable it passes in. Also important to remember is that the iterable doesn't need to be a list!&#x20;

```
>>> lst = [1, 2, 3]
>>> lst.extend(range(5))
>>> lst
[1, 2, 3, 0, 1, 2, 3, 4]
```

### Sum

Unlike `append` and `extend`, which are mutating functions, `sum` is our first aggregating function. It accepts a list and a starting value, and returns the sum of all the elements of the list and the starting value.

```python
>>> lst = [1, 2, 3]
>>> sum(lst, 10)
16
```

Note that the starting value is optional.

```python
>>> lst = [1, 2, 3]
>>> sum(lst)
6
```

What happens when the list is made up of different types of elements? For example:

```python
>>> lst = [1, 2, 's']
>>> sum(lst)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
```

Therefore, we have to be careful using the sum function, lest we run into unexpected errors like the one above!&#x20;

### Any

`any` accepts a list of elements and returns True if any of the elements are truth-y (that is, evaluate to true) and False otherwise. For more about truth-y and false-y values, read [here](/cs61a-guidebook/building-blocks/expressions-in-python/data-types-in-python.md#bools).&#x20;

```python
>>> lst = [True, False, False]
>>> any(lst)
True
>>> lst = [False, False, False]
>>> any(lst)
False
>>> lst = [2, 3, 0]
>>> any(lst)
True
>>> lst = [0, None, ""]
>> any(lst)
False
```

Keep in mind `any` function's behavior with this edge case:

```
>>> lst = []
>>> any(lst)
False
```

### All

`all` is the complement to `any.` `all` accepts a list of elements returns True if every element evaluates to True, and False otherwise.

```python
>>> lst = [True, True, False]
>>> all(lst)
False
>>> lst = [True, True, True]
>>> all(lst)
True
>>> lst = [2, 3, 0]
>>> all(lst)
False
>>> lst = [2, 3, 4]
>>> all(lst)
True
```

Keep in mind `all` function's behavior with this edge case:

```
>>> lst = []
>>> all(lst)
True
```

### max

`max` accepts a list of values, and given this list, returns the maximum value of that list.&#x20;

```python
>>> max([1, 2, 3])
3
>>> max([1, 2, -1])
2
>>> max([1, 2, 's'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
```

Another error to keep in mind is the following:

```python
>>> max([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
```

That is to say, `max` cannot be given an empty list.

The `min` function works exactly like the `max` function, with the same error cases, with the critical difference of course being that it returns the minimum value instead of the maximum one.&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnotes.gitbook.io/cs61a-guidebook/structures-of-data/iterables/iterable-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
