Python · Lists
Introduction to Lists
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Introduction to Lists
In Python, a list is a collection of items that can store different types of data. Lists are ordered, changeable, and allow duplicate values.
Lists are created using square brackets
[ ]
, with each item separated by a comma. For example:
# A list of Codémon names codemon_list = ["Aquachu", "Pyrochu", "Florachu"] print(codemon_list) # A list of different data types mixed_list = ["Aquachu", 10, True, 15.5] print(mixed_list)
Lists are very useful when you need to store multiple related values together, like your collection of Codémon or a team's statistics.
Storing different data types
In Python, lists can store different types of data together. But it doesn't mean you should do it! It's usually not a good idea, but if you really want to do it, be careful when using them, as mixing types can lead to unexpected behavior in your code.
Exercise 2 — Creating Lists
The advised way to use lists in Python is to have all the values in it of the same type (strings, numbers, booleans). Here are some examples:
# 1. Empty list empty_list = [] print("Empty list:", empty_list) # 2. List with values codemon_team = ["Aquachu", "Pyrochu", "Florachu"] print("Codémon team:", codemon_team) # 3. List of numbers levels = [5, 10, 15, 20, 25] print("Levels:", levels)
Exercise 3 — Creating Your Codémon Team
Complete the code to create lists representing your Codémon team and their levels.
# Create a list of 3 Codémon names codemon_names = ____ print(codemon_names) # Create a list of 3 Codémon level codemon_levels = ____ print(codemon_levels)
Options: ["Aquachu", "Pyrochu", "Florachu"] [10, 15, 12] "Aquachu", "Pyrochu", "Florachu" 10, 15, 12 ("Aquachu", "Pyrochu", "Florachu") (10, 15, 12)
Hint : Lists in Python are created using square brackets [ ] with comma-separated values.
Exercise 4 — Accessing List Elements
In Python, similarly to strings, you can access list elements using their index (position) in the list. The first element is at index 0, the second at index 1, and so on.
codemon_team = ["Aquachu", "Pyrochu", "Florachu"] # Access the first Codémon (index 0) first_codemon = codemon_team[0] print("First Codémon:", first_codemon) # Access the second Codémon (index 1) second_codemon = codemon_team[1] print("Second Codémon:", second_codemon) # Access the third Codémon (index 2) third_codemon = codemon_team[2] print("Third Codémon:", third_codemon)
Remember that in Python (and most programming languages), list indices start at 0, not 1. So the first element is at index 0, the second at index 1, and so on.
Index Out of Range
If you try to access an index that doesn't exist, Python will raise an "IndexError". For example, if your list has 3 items (indices 0, 1, and 2), trying to access index 3 will cause an error.
Exercise 5 — Accessing List Elements
Given the following list of Codémon types:
codemon_types = ["water", "fire", "grass", "electric", "rock"]
Which code would correctly access the "grass" type?
Options
• ```codemon_types[2]```
• ```codemon_types[3]```
• ```codemon_types[1]```
• ```codemon_types[0]```
Hint : Remember: In Python, list indices start at 0. So the first element is at index 0, the second at index 1, and so on.
Exercise 6 — Negative Indexing in Lists
Python also allows negative indexing for lists. This means you can access elements from the end of the list:
-
-1
refers to the last element -
-2
refers to the second-to-last element - and so on...
codemon_team = ["Aquachu", "Pyrochu", "Florachu"] # Access the last Codémon last_codemon = codemon_team[-1] print("Last Codémon:", last_codemon) # Access the second-to-last Codémon second_last = codemon_team[-2] print("Second-to-last Codémon:", second_last) # Access the third-to-last Codémon third_last = codemon_team[-3] print("Third-to-last Codémon:", third_last)
Negative indexing is especially useful when you don't know the exact length of the list but want to access elements from the end.
Exercise 7 — Match the Index to the Element
Given the list:
moves = ["Water Splash", "Fire Blast", "Leaf Storm", "Thunder Shock", "Rock Throw"]
Match each index with the element it would access:
Items to match
• moves[0]
• moves[2]
• moves[-1]
• moves[-3]
Definitions
• "Water Splash"
• "Leaf Storm"
• "Rock Throw"
• "Leaf Storm"
Exercise 8 — List Slicing
Similarly to strings, in addition to accessing individual elements, Python allows you to extract a portion of a list using slicing. The syntax for slicing is
list[start:end:step]
.
Important notes about slicing: - The
start
index is included - The
end
index is excluded - If you omit
start
, it defaults to 0 - If you omit
end
, it defaults to the length of the list - If you omit
step
, it defaults to 1
codemon_types = ["water", "fire", "grass", "electric", "rock", "ice"] # Get the first three types first_three = codemon_types[0:3] print("First three types:", first_three) # Shorthand for first three types also_first_three = codemon_types[:3] print("Also first three types:", also_first_three) # Get types from index 2 to the end from_third = codemon_types[2:] print("From third to the end:", from_third) # Get types from index 1 to 4 (excluding 4) middle_types = codemon_types[1:4] print("Middle types:", middle_types) # Comment blabla step_types = codemon_types[0:6:2] print("Every other type:", step_types)
Exercise 9 — Practice List Slicing
Complete the code to extract specific portions of the Codémon team list using slicing.
Here is the goal: - Get the first three Codémon - Get the last two Codémon - Get the middle three Codémon (indices 1, 2, 3)
team = ["Aquachu", "Pyrochu", "Florachu", "Rockachu", "Zap"] # Get the first three Codémon starters = team[____:____] print(starters) # Get the last two Codémon specialists = team[____:] print(specialists) # Get the middle three Codémon (indices 1, 2, 3) mid_team = team[____:____] print(mid_team)
Options: 0 3 4 1 2 -2 -3
Hint : Remember: In slicing, the start index is included and the end index is excluded. Negative indices count from the end of the list.
Exercise 10 — What Will This Slice Return?
elements = ["water", "fire", "earth", "wind", "light", "dark"]
What will the expression
elements[1:4]
return?
Options
• ["fire", "earth", "wind"]
• ["water", "fire", "earth"]
• ["fire", "earth", "wind", "light"]
• ["water", "fire", "earth", "wind"]
Hint : Remember: In list slicing, the first index is included and the second index is excluded.
Exercise 11 — Match the Code to Its Result
moves = ["Bubble", "Ember", "Vine Whip", "Thunder Shock", "Growl"]
Match each code snippet with the result it would produce:
Items to match
• moves[0]
• moves[-1]
• moves[1:3]
• moves[:2]
Definitions
• "Bubble"
• "Growl"
• ["Ember", "Vine Whip"]
• ["Bubble", "Ember"]
Exercise 12 — List Challenge - Tournament Team Selection
You're organizing pairs for a Codémon tournament! You have a list of 4 Codémon and need to create different pair configurations using indexing and slicing. Your tasks: 1. Get the first Codémon (will be the team captain) 2. Get the last Codémon (will be the reserve) 3. Create Pair A: the first 2 Codémon 4. Create Pair B: the last 2 Codémon Use the appropriate indexing and slicing techniques for each task!
Starter code
# Tournament participants codemon_roster = ["Aquachu", "Pyrochu", "Florachu", "Zap"] # Task 1: Get the team captain (first Codémon) # Task 2: Get the reserve (last Codémon) # Task 3: Create Pair A (first 2 Codémon) # Task 4: Create Pair B (last 2 Codémon)
Back to the course