By the end of this lesson, you will:
Definition: A list is a collection of values in a variable - like a digital container that can hold many items!
Think of a list like a shopping list or a playlist - it helps you organize multiple items together!
In many other programming languages, lists are known as arrays.
List is a type of data structure that allows us to put multiple values within a variable.
You can have one or more items inside a list.
:information_source: Remember! Lists are super useful when you want to store related items together, like:
- Names of your friends
- Your favorite games
- Scores in a game
- Items in an inventory
Just like house numbers on a street, each item in a list has its own "address" called an index!
The position order is important! This is stored with an index number for each position. A list will not forget the order of the items inside.
An index number always counts from 0 (not 1!).
:bulb: Pro Tip!
Remember: The first item is always at index 0, not 1! This is a common mistake for beginners.
- First item -> Index 0
- Second item -> Index 1
- Third item -> Index 2
- And so on...
Let's build our first list! Follow these steps:
Inside a list, each item is separated with a comma (,) in between.
A square bracket [] is used for lists only.
:information_source: Python Connection :emoji: In Python text code, creating a list looks like this:
python
my_list = ["apple", "banana", "orange"]
The blocks you're using create the exact same code!
Now let's learn how to get specific items from our list!
Input | Output |
---|---|
![]() |
![]() |
:bulb: Troubleshooting :wrench:
Getting an "index out of range" error? This means you're trying to access an item that doesn't exist!
- Check how many items are in your list
- Remember the last item's index is always (total items - 1)
List methods are like special tools that help you modify your lists - add items, remove items, and more!
:information_source: Remember! Each method does something different to your list. Think of them as different actions you can perform:
- Append = Add to the end
- Insert = Add at a specific spot
- Remove = Take out an item
- Pop = Remove from a position
- And more!
Sometimes you need to change an item that's already in your list. Here's how:
Input | Output |
---|---|
![]() |
![]() |
:bulb: Important!
The index position cannot be one that is outside the range of the list. If there's only 3 items in the list, you cannot set a value to index 3 and above. This is because 3 items means index 0, 1, and 2 only.
Quick Check:
- 3 items in list = Valid indexes: 0, 1, 2
- 5 items in list = Valid indexes: 0, 1, 2, 3, 4
The append method is perfect when you want to add something new to the end of your list!
Input | Output |
---|---|
![]() |
![]() |
:information_source: Python Connection :emoji: In Python, append looks like:
my_list.append("new item")
Want to add an item in the middle of your list? Insert is your friend!
Input | Output |
---|---|
![]() |
![]() |
:bulb: Pro Tip!
When you insert, all items after that position shift one spot to the right!
Need to combine two lists together? Extend makes it easy!
Input | Output |
---|---|
![]() |
![]() |
:information_source: Remember! Extend adds all items from the second list to the end of the first list. It's different from append - append would add the whole list as one item!
Input | Output |
---|---|
![]() |
![]() |
Input | Output |
---|---|
![]() |
![]() |
Input | Output |
---|---|
![]() |
![]() |