Python ยท Python Collections

Lists: Practice Questions

100 questions. Try each one yourself before checking the answer.

Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.

Q1Creating ListsEasyMust Do

Create a list called fruits holding "apple", "banana" and "cherry", then print the whole list.

Q2Creating ListsEasy

Create an empty list called basket and print it, then print its length. An empty list is where almost every list you build will start.

Q3Creating ListsEasy

Create a list called mixed containing an int, a float, a string and a bool, then print it. Unlike many languages, one Python list can hold different types at once.

Q4IndexingEasyMust Do

Using fruits = ["apple", "banana", "cherry"], print the first item and the third item. Counting starts at 0, exactly as it did for strings.

Q5IndexingEasy

Using the same list, print the last item using a negative index, without knowing how long the list is.

Q6len()Easy

Create numbers = [4, 8, 15, 16, 23, 42] and print how many items it holds. Then print its last item using len() to work out the index.

Q7MutabilityEasyMust Do

Create colours = ["red", "green", "blue"], change the middle item to "yellow", then print the list. Unlike a string, a list can be edited in place.

Q8SlicingEasyMust Do

Using numbers = [10, 20, 30, 40, 50], print the first three items with a slice.

Q9SlicingEasy

Using the same list, print everything from index 2 to the end, and separately the last two items.

Q10SlicingEasy

Using numbers = [1, 2, 3, 4, 5, 6, 7, 8], print every second item using a step.

Q11SlicingEasy

Using numbers = [1, 2, 3, 4, 5], print the list reversed with a slice, then confirm the original is unchanged.

Q12MembershipEasy

Using fruits = ["apple", "banana", "cherry"], print whether "banana" is in the list and whether "mango" is not in it.

Q13Adding ItemsEasyMust Do

Start with an empty list scores, add 85, 92 and 78 to it one at a time using .append(), then print it.

Q14Adding ItemsEasy

Using queue = ["Asha", "Raj"], put "Meera" at the very front using .insert(), then print the list.

Q15Adding ItemsEasy

Given a = [1, 2, 3] and b = [4, 5], add every item of b onto the end of a using .extend(), then print a.

Q16Adding ItemsEasy

Given a = [1, 2, 3] and b = [4, 5], build a new list holding both, using +. Print the new list and show that a is unchanged.

Q17Adding ItemsEasy

Create a list holding 0 repeated eight times, using *, then print it.

Q18Removing ItemsEasy

Using fruits = ["apple", "banana", "cherry"], remove "banana" by name with .remove(), then print the list.

Q19Removing ItemsEasyMust Do

Using stack = [10, 20, 30, 40], remove and print the last item with .pop(), then remove and print the first item with .pop(0). Print the list afterwards.

Q20Removing ItemsEasy

Using numbers = [1, 2, 3, 4, 5], delete the item at index 2 with del, then delete the last two items with a slice, printing after each step.

Q21Removing ItemsEasy

Using items = ["a", "b", "c"], empty the list completely with .clear(), then print it and its length.

Q22SearchingEasy

Using animals = ["cat", "dog", "bird", "dog"], print the position of the first "dog" using .index(), and how many dogs there are using .count().

Q23SortingEasyMust Do

Using numbers = [42, 7, 19, 3, 25], sort the list into ascending order with .sort() and print it.

Q24SortingEasy

Using names = ["Raj", "Asha", "Meera", "Dev"], sort them into reverse alphabetical order, then separately flip a list end-for-end using .reverse().

Q25LoopingEasyMust Do

Using fruits = ["apple", "banana", "cherry"], print each fruit on its own line with a for loop.

Q26SortingMediumMust Do

Show the difference between sorted() and .sort(): sort numbers = [3, 1, 2] both ways and print the original list after each, so it is obvious which one changed it.

Q27List BuildingMediumMust Do

Build a list of the numbers 1 to 10 by starting with an empty list and appending inside a loop, then print it.

Q28List BuildingMedium

Build a list holding the squares of 1 to 10 and print it.

Q29List BuildingMedium

Given numbers = [12, 7, 30, 5, 18, 21], build a new list holding only the even ones, then print both lists.

Q30AggregationMediumMust Do

Given marks = [78, 92, 65, 88, 71], print the total, the highest and the lowest using sum(), max() and min().

Q31AggregationMedium

Given the same marks, print the average to two decimal places, and how many marks are above that average.

Q32AggregationMedium

Find the largest value in numbers = [14, 8, 33, 21, 5] without using max(), by looping and tracking a running best.

Q33LoopingMedium

Given fruits = ["apple", "banana", "cherry"], print each item with its position, like 1. apple, using range(len()).

Q34Strings & ListsMediumMust Do

Ask the user for a sentence, split it into a list of words with .split(), then print how many words there are and the list itself.

Q35Strings & ListsMedium

Given words = ["Python", "is", "fun"], join them back into a sentence with spaces, and separately into a dash-separated slug.

Q36Strings & ListsMedium

Given csv_line = "Asha,22,Pune", split it on commas and print each field on its own labelled line.

Q37Strings & ListsMedium

Turn the word "python" into a list of its individual characters using list(), then print the list and its length.

Q38Nested ListsMedium

Create a 3-row grid grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Print the whole grid, then the middle row, then the number 6.

Q39Nested ListsMedium

Print the same 3x3 grid as a neat square, one row per line with the numbers spaced out.

Q40Nested ListsMedium

Using the same grid, print the total of every number in it.

Q41Nested ListsMedium

Given students = [["Asha", 88], ["Raj", 74], ["Meera", 91]], print each student's name and mark on one line, then the class average.

Q42CopyingMediumMust Do

Create a = [1, 2, 3] and then b = a. Append 4 to b and print both lists. Explain the surprise in a comment.

Q43CopyingMedium

Fix the previous problem two ways: make a real copy using .copy(), and again using a full slice [:]. Prove the original stays unchanged.

Q44CopyingMedium

Prove the difference with is: show that b = a gives the same object while a.copy() gives a different one, even though both compare equal with ==.

Q45SlicingMedium

Using numbers = [1, 2, 3, 4, 5], replace the middle three items with [30, 40] using slice assignment, then print the list and its new length.

Q46List BuildingMedium

Given numbers = [3, 7, 3, 2, 7, 8, 3], build a new list with the duplicates removed, keeping the original order.

Q47List BuildingMedium

Given a = [1, 3, 5] and b = [2, 4, 6], build a single list that alternates between them: [1, 2, 3, 4, 5, 6].

Q48Functions & ListsMedium

Write a function double_all(numbers) that returns a new list with every value doubled, leaving the original alone. Prove the original is unchanged.

Q49Functions & ListsMedium

Write a function add_score(scores, value) that appends to the list it is given and returns nothing. Show that the caller's list is changed.

Q50Functions & ListsMediumMust Do

Here is the trap promised back in Topic 7. This function is meant to start a fresh cart each time, but the second call keeps the first call's item. Explain why, and fix it.

def add_item(item, cart=[]):
    cart.append(item)
    return cart
 
print(add_item("apple"))
print(add_item("banana"))
Q51Real-WorldMediumMust Do

Ask how many marks to enter, collect them into a list in a loop, then print the total, average, highest and lowest in a small report.

Q52Real-WorldMedium

Given marks = [88, 32, 74, 19, 95, 45], split them into two lists โ€” passed (40 or above) and failed โ€” then print both with counts.

Q53Strings & ListsMedium

Ask for a sentence and print its longest word, along with how many letters that word has.

Q54SearchingMediumMust Do

Ask the user for a name and report its position in names = ["Asha", "Raj", "Meera", "Dev"], or say it is not in the list. Do not let a missing name crash the program.

Q55Strings & ListsMedium

Ask for a sentence and print how many of its words start with a vowel, plus the list of those words.

Q56AggregationMedium

Given temps = [31, 28, 35, 22, 30, 26, 33], print the average, then two lists: the days above average and the days below it.

Q57AggregationMedium

Find the second largest value in numbers = [12, 45, 8, 45, 33, 27] without sorting, handling the repeated 45 correctly.

Q58SearchingMedium

Given a = [1, 2, 3, 4, 5] and b = [4, 5, 6, 7], build a list of the values that appear in both, with no duplicates.

Q59SearchingMedium

Using the same two lists, build a list of the values that are in a but not in b, and another for the reverse.

Q60SlicingMedium

Write rotate_left(items, n) that moves the first n items to the end, so rotating [1, 2, 3, 4, 5] by 2 gives [3, 4, 5, 1, 2]. Make it survive an n larger than the list.

Q61SlicingMedium

Write chunk(items, size) that splits a list into groups of size, so chunk([1,2,3,4,5,6,7], 3) gives [[1,2,3],[4,5,6],[7]].

Q62Nested ListsMedium

Given nested = [[1, 2], [3, 4, 5], [6]], build a single flat list holding every value, and print it with its length.

Q63Nested ListsMediumMust Do

Given a 3x3 grid, print the total of each row and the total of each column.

Q64Nested ListsMedium

Transpose a grid โ€” turn its rows into columns โ€” so [[1,2,3],[4,5,6]] becomes [[1,4],[2,5],[3,6]].

Q65List BuildingMedium

Given sales = [100, 250, 75, 300], build a list of running totals, so the answer is [100, 350, 425, 725].

Q66Real-WorldMediumMust Do

Given two matching lists โ€” items = ["Pen", "Notebook", "Bag"] and prices = [15.5, 60.0, 450.0] โ€” print an aligned receipt with a total line.

Q67SearchingMediumMust Do

Given grades = ["A", "B", "A", "C", "B", "A", "D"], count how many of each grade there are using two parallel lists, and print the tally.

Q68Removing ItemsMedium

Write remove_all(items, value) that removes every occurrence of a value and returns a new list, since .remove() only deletes the first one.

Q69SortingMedium

Write insert_sorted(items, value) that inserts a value into an already-sorted list so it stays sorted, without calling .sort() afterwards.

Q70AlgorithmsMedium

Merge two already-sorted lists into one sorted list, without using .sort() or sorted().

Q71DebuggingHardMust Do

This is meant to remove every 2, but one survives. Work out why, then fix it two different ways.

numbers = [1, 2, 2, 3, 4]
 
for n in numbers:
    if n == 2:
        numbers.remove(n)
 
print(numbers)
Q72Nested ListsHardMust Do

This should set only the top-left cell to 9, but a whole column changes. Explain and fix it.

grid = [[0] * 3] * 3
grid[0][0] = 9
 
for row in grid:
    print(row)
Q73SortingHard

This prints None instead of the sorted list. Explain why and give both correct versions.

numbers = [3, 1, 2]
result = numbers.sort()
print(result)
Q74CopyingHard

+= and = x + y look interchangeable but behave differently on lists. Demonstrate both with an alias watching, and explain the difference.

Q75SlicingHard

Show that an out-of-range slice is harmless while an out-of-range index is fatal, and explain when that difference helps you.

Q76Removing ItemsHard

Show the two ways .remove() surprises people: it deletes only the first match, and it crashes on a value that is not there. Write a safe helper that never crashes.

Q77SortingHard

Explain why sorting a list of mixed types fails, and show two ways to sort ["10", "9", "100"] sensibly.

Q78DebuggingHard

This is meant to delete every even number using .pop(), but it misses some. Find the bug and fix it.

numbers = [1, 2, 4, 6, 7, 8]
 
i = 0
while i < len(numbers):
    if numbers[i] % 2 == 0:
        numbers.pop(i)
    i += 1
 
print(numbers)
Q79SearchingHard

Write linear_search(items, target) that returns the index of the target or -1 if it is absent, without using .index() or in.

Q80SearchingHardMust Do

Write binary_search(items, target) for an already-sorted list, halving the search range each step. Return the index or -1.

Q81AlgorithmsHard

Sort a list using bubble sort: repeatedly walk the list swapping neighbours that are in the wrong order. Print how many passes it took.

Q82AlgorithmsHard

Sort a list using selection sort: find the smallest remaining item and swap it into place.

Q83AlgorithmsHard

Reverse a list in place without using .reverse() or [::-1], by swapping from both ends inwards.

Q84AlgorithmsHard

Given a sorted list and a target, find a pair of values that add up to the target using two pointers, without nested loops.

Q85AlgorithmsHard

Move every zero in numbers = [0, 3, 0, 5, 9, 0, 2] to the end in place, keeping the other values in their original order.

Q86Mini-ProjectMini-ProjectMust Do

Build a To-Do List Manager. Offer a menu to add a task, remove a task by number, view all tasks numbered, and quit. Keep running until the user exits, and refuse to remove from an empty list.

Q87Mini-ProjectMini-ProjectMust Do

Build a Student Report Card. Store each student as a small list of [name, mark1, mark2, mark3] inside one outer list, then print a table with each student's total, average and grade, plus the class average.

Q88Mini-ProjectMini-Project

Build an Inventory Tracker using two parallel lists for item names and quantities. Offer add stock, sell stock (refusing to go negative), and a report showing which items are low (under 5).

Q89Mini-ProjectMini-Project

Build a Number Statistics Report. Keep reading numbers until the user types done, then print count, total, average, highest, lowest, the sorted list, how many were above average, and the range.

Q90Mini-ProjectMini-Project

Build a Word Frequency Counter. Ask for a sentence, then report each distinct word and how many times it appeared, sorted so the most frequent comes first.

Q91Mini-ProjectMini-Project

Build an Attendance Register. Store a grid where each row is a student and each column is a day, using 1 for present and 0 for absent. Print each student's attendance percentage and each day's headcount.

Q92Mini-ProjectMini-Project

Build a Queue Simulator for a ticket counter. Offer join the queue, serve the next person (always the one who has waited longest), and show who is waiting with their position.

Q93Mini-ProjectMini-Project

Build a Matrix Calculator. Write add_matrices(a, b) and multiply_matrices(a, b), plus a show(matrix) helper that prints a grid with aligned columns.

Q94Mini-ProjectMini-Project

Build a Tic-Tac-Toe Checker. Store the board as a 3x3 grid, print it with dividers, and write winner(board) that returns "X", "O" or "none" by checking all rows, columns and both diagonals.

Q95Mini-ProjectMini-Project

Build a Shopping Cart. Keep adding item names and prices until the user types done, then print an itemised receipt with the subtotal, the most expensive item, a 5% discount if the subtotal is over 1000, and the final total.

Q96InterviewInterview

Explain what it means that lists are mutable and strings are not. Demonstrate the difference in three ways: editing by index, what a method returns, and what happens when a function receives one.

Q97CopyingInterview

Explain the difference between a shallow and a deep copy. Show that .copy() is not enough for a list of lists, then write a deep copy by hand.

Q98SortingInterview

Explain why list.sort() returns None while sorted() returns a list, and say when you would reach for each.

Q99InterviewInterview

Not every list operation costs the same. Explain why .append() is cheap but .insert(0, x) and in are not, and demonstrate the scanning cost by counting steps.

Q100CapstoneInterviewMust Do

Capstone. Build a List Toolkit Report. Read numbers until the user types done, then print one report showing: the original list, the sorted copy (original preserved), the reverse, the duplicates removed, the evens, the running totals, the second largest, the list split into chunks of three, and the full statistics.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session