Analyse Lists

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Analyzing Lists

Now that you know how to add and remove elements, let's learn how to analyze your lists! In this chapter, we'll explore four essential methods and functions for getting information about lists:

• len() - Get the number of items in a list • index() - Find the position of a specific item • count() - Count how many times an item appears • in - Check if an item exists in a list

These tools are essential for understanding and working with your lists. Let's use our Codémon examples to see how they work!

Exercise 2 — The len() Function

The len() function returns the number of items in a list. It's very useful to check how many elements you have!

# Your Codémon team my_team = ["Zap", "Aquachu", "Pyrochu"] print("My team:", my_team) print("My team size:", len(my_team)) print("-" * 40) # Enemy team enemy_team = ["Pyrochu"] print("Enemy team:", enemy_team) print("Enemy team size:", len(enemy_team)) print("-" * 40) # Empty team (just started the game!) empty_team = [] print("Empty team:", empty_team) print("Empty team size:", len(empty_team))

len() works with many types!

The len() function can be used with other data types too, like strings, tuples, and dictionaries (you'll learn about those later). It's one of the most useful functions in Python!

Exercise 3 — Understanding len()

What will be the output of the following code?

codemons = ["Aquachu", "Pyrochu", "Florachu", "Rockachu"] print(len(codemons))
Options
• 3
• 4
• 5
• Error

Hint : The len() function counts the number of items in the list.

Exercise 4 — The index() Method

The index() method finds the position of an element in a list. It returns the index of the first occurrence of the element.

Basic syntax: list.index(element, start, end) - element: Required - the item to search for - start: Optional - where to start the search (default is 0) - end: Optional - where to end the search (default is the end of the list)

codemons = ['Aquachu', 'Pyrochu', 'Florachu', 'Aquachu', 'Pyrochu'] # Find the first occurrence of 'Aquachu' first_aquachu = codemons.index('Aquachu') print("First Aquachu is at index:", first_aquachu) # Find 'Aquachu' starting from index 1 second_aquachu = codemons.index('Aquachu', 1) print("Second Aquachu is at index:", second_aquachu) # Find 'Pyrochu' between indices 2 and 5 second_pyrochu = codemons.index('Pyrochu', 2, 5) print("Second Pyrochu is at index:", second_pyrochu)

What if the element is not found?

If the element is not found in the list, Python will raise a ValueError. It's a good practice to check if an element is in the list before using index() with the 'in' operator.

Exercise 5 — Using the index() Method

1. Find the position of the first "Pyrochu". 2. Find the position of the second "Aquachu" (after the first one).

Hint : Remember: The index() method returns the position of the first occurrence of an element. To find subsequent occurrences, use the start parameter to begin searching after the first occurrence.

Exercise 6 — The count() Method

The count() method returns the number of times a specified element appears in a list.

Basic syntax: list.count(element) - element: The item to count occurrences of

codemons = ['Aquachu', 'Pyrochu', 'Florachu', 'Aquachu', 'Pyrochu', 'Aquachu'] # Count how many Aquachu are in the team aquachu_count = codemons.count('Aquachu') print("Number of Aquachu:", aquachu_count) # Count how many Florachu are in the team florachu_count = codemons.count('Florachu') print("Number of Florachu:", florachu_count) # Count a Codémon that isn't in the team test_count = codemons.count('Test') print("Number of Test:", test_count) # Returns 0 instead of an error

Exercise 7 — Using the count() Method

What will be the output of the following code?

numbers = [1, 2, 3, 1, 0] print(numbers.count(1) + numbers.count(0))
Options
• 3
• 4
• 5
• 2

Hint : Remember: In Python, count() counts the occurrences of a specified value in a list.

Exercise 8 — The 'in' Operator for Lists

The in operator allows you to check if an item exists in a list. It returns True if the item is in the list, and False if it's not.

# A list of Codémon types codemon_types = ["water", "fire", "grass", "electric"] # Check if "fire" is in the list print("fire" in codemon_types) # Check if "rock" is in the list print("rock" in codemon_types)

You can also use not in to check if an item is NOT in the list:

codemon_types = ["water", "fire", "grass", "electric"] # Check if "rock" is not in the list print("rock" not in codemon_types)

The 'in' operator is versatile

The in operator works with many Python sequences, not just lists. You can use it with strings, tuples, sets, and dictionaries.

Exercise 9 — Using the 'in' Operator

Given the following list, what will be the result of the expression?

codemon_collection = ["Aquachu", "Pyrochu", "Florachu"] print("Pyrochu" in codemon_collection)
Options
• True
• False
• "Pyrochu"
• 1

Hint : Remember: The in operator checks if an item exists in a list and returns a boolean value.

Exercise 10 — Checking for Codémon Types

1. Check if the desired Codémon type is available in your collection. 2. Check if the "rock" type is not in your collection.

Hint : Remember: Use in to check if an element is in a list, and not in to check if it's not in the list.

Exercise 11 — Match Analysis Methods

Match each method or function with its correct description.

Items to match
• len(team)
• team.index('Aquachu')
• team.count('Pyrochu')
• team.index('Florachu', 2)
• 'Zap' in team
Definitions
• Returns the number of items in the list
• Finds the position of the first 'Aquachu'
• Counts how many 'Pyrochu' are in the list
• Finds 'Florachu' starting search from index 2
• Checks if 'Zap' exists in the list

Exercise 12 — Analyzing a Team

You need to analyze a Codémon team. Which code correctly: 1. Gets the total number of Codémon 2. Finds the position of 'Florachu' 3. Counts how many 'Aquachu' are in the team 4. Checks if 'Zap' is in the team

Options
• total = len(team) position = team.index('Florachu') count = team.count('Aquachu') has_zap = 'Zap' in team
• total = team.size() position = team.index('Florachu') count = team.count('Aquachu') has_zap = 'Zap' in team
• total = len(team) position = team.find('Florachu') count = team.count('Aquachu') has_zap = team.has('Zap')
• total = team.length() position = team.index('Florachu') count = team.count('Aquachu') has_zap = team.contains('Zap')

Hint : Remember: len() gets the size, index() finds the position, count() counts occurrences, and in checks membership.

Exercise 13 — Challenge - Team Statistics

You're a Codémon trainer and need to analyze your team! Your tasks: 1. Count the total number of Codémon in your team 2. Find the position of "Florachu" in your team 3. Count how many "Aquachu" you have 4. Count how many "Pyrochu" you have 5. Check if "Zap" is in your team Use len(), index(), count(), and in to complete this challenge!

Starter code
# Your Codémon team my_team = ["Aquachu", "Pyrochu", "Florachu", "Aquachu", "Zap", "Pyrochu", "Aquachu"] print("My team:", my_team) print("=" * 50) # Task 1: Count total Codémon # Task 2: Find position of Florachu # Task 3: Count Aquachu # Task 4: Count Pyrochu # Task 5: Check if Zap is in the team
Back to the course