---
# System prepended metadata

title: 'Lists: Constructing Lists'

---

# Lists: Constructing Lists

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/Sy7AHDNn8) ... [next](https://hackmd.io/@alexhkurz/ByiUweLfD) ... )

In the previous lecture we saw how to implement in Python programs such as

    length
    add
    mulitply
    average
    
by iteration over a given list.

**Question:** Which of the four programs makes sense for any list? Which other three programs require that we know that the elements of the list are numbers?

**Question:** Which other programs on lists can you think of that make sense independently of the type of the elements?

Here is an example: Reversing a list.

The task is, for a given list `[a_0, ... a_n]`, to construct a new list `[a_n, ... a_0]`.

**Activity:** Write a Python function that reverses a list.

- We start with a given list, which I call the old list.
- To construct a new list, we can start with the empty list `[]`.
- Then we add elements to the new list, step by step, iterating over the old list using a `for`-loop.
- To make lists bigger, we can use concatenation. For example

    ``[1,2,3] + [4,5]`` returns `[1,2,3,4,5]`
    
Is that enough to get you going?

