# 02 - Lists ## Video <iframe width="720" height="406" src="https://www.youtube.com/embed/NPLB-xav8a8?si=r__XXCooM6EhijpE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> --- ## What is a List? A [**list**](/glossary/coding-basics-py/list) is a way to hold many things in one variable. In the last video, `poem` held one string ("The Raven"). Here we're going to create a list called `poems` that can include many strings. You'll probably often hear me and other people accidentally call these **arrays** if we come from another programming language. In JavaScript, we would call these things arrays. But in Python, the official name is **list**. If you want to sound like a cool Python developer, get in the habit of saying "list." --- ## Creating a List ```python poems = ["The Raven", "Ode to a Nightingale", "Hope is the Thing with Feathers"] print(poems) ``` Output: ``` ['The Raven', 'Ode to a Nightingale', 'Hope is the Thing with Feathers'] ``` Here's how to create a list: - Use **square brackets** `[]` around the content - Put comma-separated items inside - It can be as long as you want - You can put different types of things in there (strings, numbers, etc.) When we print it, we see that whole list—it's a package of these three things. --- ## Accessing Items by Index If you want to access just one of the things in the list, use this syntax: ```python print(poems[0]) # The Raven print(poems[1]) # Ode to a Nightingale print(poems[2]) # Hope is the Thing with Feathers ``` We take the name of the variable (`poems`), then use square brackets right after it (no spaces), and put in the [**index**](/glossary/coding-basics-py/index) of whatever item we're looking for. ### Zero-Indexed The thing to remember if you're new to this: lists start with **zero**, not one. - The first element is `[0]` - The second element is `[1]` - The third element is `[2]` Tricky, but eventually you'll get the hang of it—you just have to add one or subtract one in your head sometimes. It's actually pretty rare that you'll be specifically grabbing element number seven or something like that. Ultimately we're mainly going to be doing something to **all** the elements of the list (that's what loops are for!). But if you ever do want to get just one specific element, this is how. --- ## Getting the Length There are some functions you can run on lists. One of these is `len()` (length): ```python print(len(poems)) # 3 ``` This tells you how many items are in the list. Very useful! --- ## Adding Items You can add new items to a list using `.append()`: ```python poems.append("To a Skylark") print(len(poems)) # Now it's 4 ``` --- ## Try It 1. Create your own list with several strings 2. Try to access specific elements using their index 3. Use `len()` to find out how many items are in your list 4. Try adding an item with `.append()` --- ## Summary In this notebook, you learned: - A **list** holds multiple items in one variable - Use **square brackets** `[]` to create a list - Access items by **index** (starting at 0) - Use `len()` to get the number of items - Use `.append()` to add items Next up: **loops** — where the magic really happens!