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.
Create a list called fruits holding "apple", "banana" and "cherry", then print the whole list.
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.
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.
Using fruits = ["apple", "banana", "cherry"], print the first item and the third item. Counting starts at 0, exactly as it did for strings.
Using the same list, print the last item using a negative index, without knowing how long the list is.
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.
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.
Using numbers = [10, 20, 30, 40, 50], print the first three items with a slice.
Using the same list, print everything from index 2 to the end, and separately the last two items.
Using numbers = [1, 2, 3, 4, 5, 6, 7, 8], print every second item using a step.
Using numbers = [1, 2, 3, 4, 5], print the list reversed with a slice, then confirm the original is unchanged.
Using fruits = ["apple", "banana", "cherry"], print whether "banana" is in the list and whether "mango" is not in it.
Start with an empty list scores, add 85, 92 and 78 to it one at a time using .append(), then print it.
Using queue = ["Asha", "Raj"], put "Meera" at the very front using .insert(), then print the list.
Given a = [1, 2, 3] and b = [4, 5], add every item of b onto the end of a using .extend(), then print a.
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.
Create a list holding 0 repeated eight times, using *, then print it.
Using fruits = ["apple", "banana", "cherry"], remove "banana" by name with .remove(), then print the list.
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.
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.
Using items = ["a", "b", "c"], empty the list completely with .clear(), then print it and its length.
Using animals = ["cat", "dog", "bird", "dog"], print the position of the first "dog" using .index(), and how many dogs there are using .count().
Using numbers = [42, 7, 19, 3, 25], sort the list into ascending order with .sort() and print it.
Using names = ["Raj", "Asha", "Meera", "Dev"], sort them into reverse alphabetical order, then separately flip a list end-for-end using .reverse().
Using fruits = ["apple", "banana", "cherry"], print each fruit on its own line with a for loop.
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.
Build a list of the numbers 1 to 10 by starting with an empty list and appending inside a loop, then print it.
Build a list holding the squares of 1 to 10 and print it.
Given numbers = [12, 7, 30, 5, 18, 21], build a new list holding only the even ones, then print both lists.
Given marks = [78, 92, 65, 88, 71], print the total, the highest and the lowest using sum(), max() and min().
Given the same marks, print the average to two decimal places, and how many marks are above that average.
Find the largest value in numbers = [14, 8, 33, 21, 5] without using max(), by looping and tracking a running best.
Given fruits = ["apple", "banana", "cherry"], print each item with its position, like 1. apple, using range(len()).
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.
Given words = ["Python", "is", "fun"], join them back into a sentence with spaces, and separately into a dash-separated slug.
Given csv_line = "Asha,22,Pune", split it on commas and print each field on its own labelled line.
Turn the word "python" into a list of its individual characters using list(), then print the list and its length.
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.
Print the same 3x3 grid as a neat square, one row per line with the numbers spaced out.
Using the same grid, print the total of every number in it.
Given students = [["Asha", 88], ["Raj", 74], ["Meera", 91]], print each student's name and mark on one line, then the class average.
Create a = [1, 2, 3] and then b = a. Append 4 to b and print both lists. Explain the surprise in a comment.
Fix the previous problem two ways: make a real copy using .copy(), and again using a full slice [:]. Prove the original stays unchanged.
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 ==.
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.
Given numbers = [3, 7, 3, 2, 7, 8, 3], build a new list with the duplicates removed, keeping the original order.
Given a = [1, 3, 5] and b = [2, 4, 6], build a single list that alternates between them: [1, 2, 3, 4, 5, 6].
Write a function double_all(numbers) that returns a new list with every value doubled, leaving the original alone. Prove the original is unchanged.
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.
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"))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.
Given marks = [88, 32, 74, 19, 95, 45], split them into two lists โ passed (40 or above) and failed โ then print both with counts.
Ask for a sentence and print its longest word, along with how many letters that word has.
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.
Ask for a sentence and print how many of its words start with a vowel, plus the list of those words.
Given temps = [31, 28, 35, 22, 30, 26, 33], print the average, then two lists: the days above average and the days below it.
Find the second largest value in numbers = [12, 45, 8, 45, 33, 27] without sorting, handling the repeated 45 correctly.
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.
Using the same two lists, build a list of the values that are in a but not in b, and another for the reverse.
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.
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]].
Given nested = [[1, 2], [3, 4, 5], [6]], build a single flat list holding every value, and print it with its length.
Given a 3x3 grid, print the total of each row and the total of each column.
Transpose a grid โ turn its rows into columns โ so [[1,2,3],[4,5,6]] becomes [[1,4],[2,5],[3,6]].
Given sales = [100, 250, 75, 300], build a list of running totals, so the answer is [100, 350, 425, 725].
Given two matching lists โ items = ["Pen", "Notebook", "Bag"] and prices = [15.5, 60.0, 450.0] โ print an aligned receipt with a total line.
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.
Write remove_all(items, value) that removes every occurrence of a value and returns a new list, since .remove() only deletes the first one.
Write insert_sorted(items, value) that inserts a value into an already-sorted list so it stays sorted, without calling .sort() afterwards.
Merge two already-sorted lists into one sorted list, without using .sort() or sorted().
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)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)This prints None instead of the sorted list. Explain why and give both correct versions.
numbers = [3, 1, 2]
result = numbers.sort()
print(result)+= and = x + y look interchangeable but behave differently on lists. Demonstrate both with an alias watching, and explain the difference.
Show that an out-of-range slice is harmless while an out-of-range index is fatal, and explain when that difference helps you.
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.
Explain why sorting a list of mixed types fails, and show two ways to sort ["10", "9", "100"] sensibly.
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)Write linear_search(items, target) that returns the index of the target or -1 if it is absent, without using .index() or in.
Write binary_search(items, target) for an already-sorted list, halving the search range each step. Return the index or -1.
Sort a list using bubble sort: repeatedly walk the list swapping neighbours that are in the wrong order. Print how many passes it took.
Sort a list using selection sort: find the smallest remaining item and swap it into place.
Reverse a list in place without using .reverse() or [::-1], by swapping from both ends inwards.
Given a sorted list and a target, find a pair of values that add up to the target using two pointers, without nested loops.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
Explain why list.sort() returns None while sorted() returns a list, and say when you would reach for each.
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.
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