Bird
Raised Fist0
Pythonprogramming~20 mins

Why lists are used in Python - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this list operation?
Look at this Python code that uses a list. What will it print?
Python
fruits = ['apple', 'banana', 'cherry']
fruits.append('date')
print(fruits)
A['date', 'apple', 'banana', 'cherry']
B['apple', 'banana', 'cherry']
C['apple', 'banana', 'cherry', 'date']
DError: append is not a list method
Attempts:
2 left
๐Ÿ’ก Hint
Remember, append adds an item to the end of the list.
๐Ÿง  Conceptual
intermediate
2:00remaining
Why use lists instead of separate variables?
Why is it better to use a list to store multiple values instead of separate variables like a1, a2, a3?
ALists make the program run faster than separate variables.
BLists prevent any changes to the stored items.
CLists automatically sort the items for you.
DLists let you store many items in one place and easily loop through them.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how you would handle many items in a program.
โ“ Predict Output
advanced
2:00remaining
What is the output after modifying a list?
What will this code print?
Python
numbers = [1, 2, 3, 4]
numbers[1] = 10
print(numbers)
A[1, 10, 3, 4]
B[10, 2, 3, 4]
C[1, 2, 3, 4]
DError: lists do not support item assignment
Attempts:
2 left
๐Ÿ’ก Hint
Remember, lists can change items by index.
โ“ Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code cause?
Python
my_list = [1, 2, 3]
print(my_list[5])
ATypeError
BIndexError
CKeyError
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens if you ask for an item beyond the list length.
๐Ÿง  Conceptual
expert
3:00remaining
Why are lists useful for dynamic data?
Imagine you are making a shopping list app. Why is a list a good choice to store the items?
ABecause lists can grow or shrink as items are added or removed.
BBecause lists automatically save data to a file.
CBecause lists prevent duplicate items from being added.
DBecause lists can only store numbers.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how your shopping list changes over time.

Practice

(1/5)
1. Why do we use lists in Python?
easy
A. To store multiple items in one variable
B. To store only one item at a time
C. To perform mathematical calculations only
D. To create functions

Solution

  1. Step 1: Understand the purpose of lists

    Lists are designed to hold many items together in one place.
  2. Step 2: Compare options with list features

    Only To store multiple items in one variable correctly states that lists store multiple items in one variable.
  3. Final Answer:

    To store multiple items in one variable -> Option A
  4. Quick Check:

    Lists hold many items = To store multiple items in one variable [OK]
Hint: Lists hold many items together in one variable [OK]
Common Mistakes:
  • Thinking lists store only one item
  • Confusing lists with functions
  • Assuming lists are for math only
2. Which of the following is the correct way to create a list in Python?
easy
A. my_list = [1, 2, 3]
B. my_list = (1, 2, 3)
C. my_list = {1, 2, 3}
D. my_list = <1, 2, 3>

Solution

  1. Step 1: Recall list syntax in Python

    Lists are created using square brackets [] around items.
  2. Step 2: Match options with correct syntax

    my_list = [1, 2, 3] uses square brackets, so it is correct. Others use parentheses, curly braces, or angle brackets which are incorrect for lists.
  3. Final Answer:

    my_list = [1, 2, 3] -> Option A
  4. Quick Check:

    Lists use [] brackets = my_list = [1, 2, 3] [OK]
Hint: Lists use square brackets [] to hold items [OK]
Common Mistakes:
  • Using parentheses () which create tuples
  • Using curly braces {} which create sets or dicts
  • Using angle brackets <> which are invalid
3. What will be the output of this code?
fruits = ['apple', 'banana', 'cherry']
print(fruits[1])
medium
A. apple
B. IndexError
C. cherry
D. banana

Solution

  1. Step 1: Understand list indexing

    Python lists start indexing at 0, so fruits[1] is the second item.
  2. Step 2: Identify the item at index 1

    The list is ['apple', 'banana', 'cherry'], so index 1 is 'banana'.
  3. Final Answer:

    banana -> Option D
  4. Quick Check:

    fruits[1] = banana [OK]
Hint: List indexes start at 0, so index 1 is second item [OK]
Common Mistakes:
  • Thinking index starts at 1
  • Confusing list with dictionary
  • Expecting an error for valid index
4. Find the error in this code that tries to add an item to a list:
my_list = [1, 2, 3]
my_list.add(4)
print(my_list)
medium
A. Missing brackets in print statement
B. Using add() instead of append() to add item
C. List cannot hold numbers
D. List must be declared with curly braces

Solution

  1. Step 1: Identify method to add items to list

    Python lists use append() to add items, not add().
  2. Step 2: Check the code for method usage

    The code uses my_list.add(4), which causes an error because add() is not a list method.
  3. Final Answer:

    Using add() instead of append() to add item -> Option B
  4. Quick Check:

    Use append() to add items to list [OK]
Hint: Use append() to add items to lists, not add() [OK]
Common Mistakes:
  • Using add() which is for sets
  • Thinking lists can't hold numbers
  • Incorrect print syntax assumptions
5. You have a list of student names and want to add a new student, remove one who left, and keep the order. Which data structure should you use and why?
hard
A. Use a dictionary because it stores key-value pairs
B. Use a set because it automatically sorts items
C. Use a list because it keeps order and allows adding/removing items
D. Use a tuple because it is immutable

Solution

  1. Step 1: Understand requirements for data structure

    The data structure must keep order and allow adding/removing students.
  2. Step 2: Match requirements with data structure features

    Lists keep order and allow adding/removing items. Sets do not keep order. Dictionaries store key-value pairs, not just items. Tuples are immutable and cannot be changed.
  3. Final Answer:

    Use a list because it keeps order and allows adding/removing items -> Option C
  4. Quick Check:

    Lists keep order and are changeable [OK]
Hint: Lists keep order and allow changes, perfect for student lists [OK]
Common Mistakes:
  • Choosing sets which don't keep order
  • Confusing dictionaries with lists
  • Using tuples which cannot be changed