Practice Questions — Lists in Python
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output of the following code?
nums = [10, 20, 30, 40]
print(nums[0])
print(nums[-1])Index 0 is the first element. Index -1 is the last element.
1040Question 2
Easy
What is the output?
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits)
print(len(fruits))append() adds an item to the end of the list.
['apple', 'banana', 'cherry', 'mango']4Question 3
Easy
What is the output?
nums = [1, 2, 3, 4, 5]
print(nums[1:4])Slicing from index 1 up to (but not including) index 4.
[2, 3, 4]Question 4
Easy
What is the output?
colors = ["red", "green", "blue"]
colors[1] = "yellow"
print(colors)Lists are mutable. You can assign a new value to any index.
['red', 'yellow', 'blue']Question 5
Easy
What is the output?
nums = [3, 1, 4, 1, 5, 9]
print(nums.count(1))
print(nums.index(4))count() counts occurrences. index() returns the position of the first match.
22Question 6
Easy
What is the output?
nums = [5, 3, 8, 1]
nums.sort()
print(nums)sort() arranges elements in ascending order by default.
[1, 3, 5, 8]Question 7
Medium
What is the output?
a = [1, 2, 3]
b = a
b.append(4)
print(a)
print(a is b)b = a does not create a copy. Both variables reference the same list.
[1, 2, 3, 4]TrueQuestion 8
Medium
What is the output?
a = [1, 2, 3]
b = a.copy()
b.append(4)
print(a)
print(b)
print(a is b).copy() creates a new list with the same elements.
[1, 2, 3][1, 2, 3, 4]FalseQuestion 9
Medium
What is the output?
nums = [10, 20, 30, 40, 50]
print(nums[::-1])
print(nums[3:0:-1])[::-1] reverses the list. [3:0:-1] goes backwards from index 3, stopping before index 0.
[50, 40, 30, 20, 10][40, 30, 20]Question 10
Medium
What is the output?
result = [x**2 for x in range(1, 6)]
print(result)List comprehension: [expression for variable in iterable].
[1, 4, 9, 16, 25]Question 11
Medium
What is the output?
evens = [x for x in range(10) if x % 2 == 0]
print(evens)List comprehension with a filter condition.
[0, 2, 4, 6, 8]Question 12
Medium
What is the output?
a = [1, 2, 3]
b = [4, 5]
a.extend(b)
print(a)
print(b)extend() adds each element from the iterable, not the iterable itself.
[1, 2, 3, 4, 5][4, 5]Question 13
Hard
What is the output?
nums = [5, 3, 1, 4]
result = nums.sort()
print(result)
print(nums).sort() returns None. It modifies the list in place.
None[1, 3, 4, 5]Question 14
Hard
What is the output?
a = [1, 2, 3]
a.append([4, 5])
print(a)
print(len(a))append() adds the entire argument as a single element.
[1, 2, 3, [4, 5]]4Question 15
Hard
What is the output?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])
print(matrix[0][-1])
print(matrix[-1][0])First index selects the row, second index selects the column.
637Question 16
Hard
What is the output?
outer = [[1, 2], [3, 4]]
shallow = outer.copy()
shallow[0][0] = 99
print(outer)
print(shallow)Shallow copy creates a new outer list, but inner lists are still shared.
[[99, 2], [3, 4]][[99, 2], [3, 4]]Question 17
Hard
What is the output?
first, *rest, last = [10, 20, 30, 40, 50]
print(first)
print(rest)
print(last)The * collects all remaining elements between first and last into a list.
10[20, 30, 40]50Question 18
Hard
What is the output?
nums = [1, 2, 3, 4, 5]
nums[1:4] = [20]
print(nums)
print(len(nums))Slice assignment can replace a section with a different number of elements.
[1, 20, 5]3Mixed & Application Questions
Question 1
Easy
What is the output?
nums = [1, 2, 3]
nums.insert(0, 0)
print(nums)insert(index, item) adds the item before the given index.
[0, 1, 2, 3]Question 2
Easy
What is the output?
nums = [10, 20, 30, 40]
popped = nums.pop(1)
print(popped)
print(nums)pop(index) removes and returns the element at that index.
20[10, 30, 40]Question 3
Easy
What is the output?
print(3 in [1, 2, 3, 4])
print(5 in [1, 2, 3, 4])
print("a" in ["a", "b", "c"])The 'in' operator checks if an item exists in the list.
TrueFalseTrueQuestion 4
Medium
What is the output?
nums = [1, 2, 3, 4, 5]
new = sorted(nums, reverse=True)
print(nums)
print(new)sorted() returns a new list. The original is unchanged.
[1, 2, 3, 4, 5][5, 4, 3, 2, 1]Question 5
Medium
What is the output?
words = ["hello", "world"]
upper = [w.upper() for w in words]
print(upper)
print(words)List comprehension creates a new list. The original list is unchanged.
['HELLO', 'WORLD']['hello', 'world']Question 6
Medium
What is the output?
nums = [4, 7, 2, 9, 1]
print(min(nums))
print(max(nums))
print(sum(nums))min(), max(), and sum() are built-in functions that work with lists of numbers.
1923Question 7
Medium
What is the output?
a = [1, 2]
b = [3, 4]
c = a + b
print(c)
print(a)+ creates a new list by concatenating. It does not modify the originals.
[1, 2, 3, 4][1, 2]Question 8
Hard
What is the output?
nums = [1, 2, 3, 4, 5, 6]
for num in nums:
if num % 2 == 0:
nums.remove(num)
print(nums)Removing elements while iterating over a list causes elements to be skipped.
[1, 3, 5, 6]Question 9
Hard
What is the output?
a = [[0]] * 3
a[0][0] = 5
print(a)Multiplying a list containing a mutable object creates multiple references to the SAME object.
[[5], [5], [5]]Question 10
Hard
What is the output?
nums = list(range(10))
result = nums[8:2:-2]
print(result)Start at index 8, go backwards by step -2, stop before index 2.
[8, 6, 4]Question 11
Medium
What is the difference between
append() and extend()? Give an example where using the wrong one causes unexpected behavior.One adds a single element, the other adds each element from an iterable.
append(item) adds item as a single element to the end of the list. extend(iterable) adds each element from the iterable individually.Example:
a = [1, 2]; a.append([3, 4]) gives [1, 2, [3, 4]] (nested list). a = [1, 2]; a.extend([3, 4]) gives [1, 2, 3, 4] (flat list).Question 12
Medium
What is the difference between
.sort() and sorted()?One modifies in place, the other returns a new list.
.sort() sorts the list in place and returns None. sorted() returns a new sorted list and leaves the original unchanged.Question 13
Hard
What is the output?
flat = [num for row in [[1,2],[3,4],[5,6]] for num in row]
print(flat)Nested comprehension: outer loop first, inner loop second.
[1, 2, 3, 4, 5, 6]Multiple Choice Questions
MCQ 1
Which of the following creates an empty list?
Answer: B
B is correct. Square brackets
B is correct. Square brackets
[] create an empty list. Parentheses () (A) create an empty tuple. Curly braces {} (C) create an empty dictionary, not a list. <> (D) is not valid Python syntax.MCQ 2
What does nums.append(5) do?
Answer: B
B is correct.
B is correct.
append() always adds the item to the end of the list. To add at the beginning, use insert(0, 5). To add at a specific index, use insert(index, 5).MCQ 3
What is [1, 2, 3] + [4, 5]?
Answer: B
B is correct. The
B is correct. The
+ operator concatenates two lists, creating a new list with all elements. It does NOT nest the second list (A) or add corresponding elements (C).MCQ 4
How do you check if 5 is in a list called nums?
Answer: C
C is correct. The
C is correct. The
in operator checks membership. Python lists do not have contains() (A), has() (B), or find() (D) methods. find() is a string method, not a list method.MCQ 5
What does .pop() return when called with no arguments?
Answer: B
B is correct.
B is correct.
pop() with no arguments removes and returns the last element. pop(0) would remove and return the first element. Unlike .sort(), .pop() does return a value.MCQ 6
What is the output of [x for x in range(5) if x % 2 == 0]?
Answer: B
B is correct. The comprehension filters values from range(5) where x is even. 0 % 2 == 0 (True), 1 % 2 == 0 (False), 2 % 2 == 0 (True), 3 (False), 4 (True). Result: [0, 2, 4]. Option C misses 0.
B is correct. The comprehension filters values from range(5) where x is even. 0 % 2 == 0 (True), 1 % 2 == 0 (False), 2 % 2 == 0 (True), 3 (False), 4 (True). Result: [0, 2, 4]. Option C misses 0.
MCQ 7
What does .sort() return?
Answer: C
C is correct.
C is correct.
.sort() sorts the list in place and returns None. This is a Python convention: methods that modify objects in place return None. Use sorted() if you need a return value.MCQ 8
What is the difference between remove() and pop()?
Answer: B
B is correct.
B is correct.
remove(value) removes the first occurrence of the specified value. pop(index) removes and returns the element at the specified index. They work in opposite ways.MCQ 9
Which creates a proper copy of list a?
Answer: B
B is correct.
B is correct.
a.copy() creates a shallow copy. Option A creates an alias (same object). Option C assigns None (sort returns None). Option D assigns None (append returns None).MCQ 10
What is the output of len([[1, 2], [3, 4], [5, 6]])?
Answer: B
B is correct.
B is correct.
len() counts the number of top-level elements. The list contains 3 inner lists, so the length is 3. It does NOT count the total number of elements across all inner lists (that would be 6).MCQ 11
What is the output of [[0] * 3 for _ in range(2)]?
Answer: A
A is correct. The comprehension creates 2 rows, each containing [0, 0, 0]. Unlike
A is correct. The comprehension creates 2 rows, each containing [0, 0, 0]. Unlike
[[0] * 3] * 2 (which creates shared references), this comprehension creates independent inner lists. This is the safe way to create a 2D list of zeros.MCQ 12
What happens when you call .remove() on a value that does not exist in the list?
Answer: C
C is correct.
C is correct.
.remove(value) raises a ValueError if the value is not found in the list. It does not return -1 like string's find(). Always check with in before calling remove() if you are unsure.MCQ 13
What is the output of [1, 2, 3] * 2?
Answer: B
B is correct. The
B is correct. The
* operator repeats the list. [1, 2, 3] * 2 creates a new list with the elements repeated: [1, 2, 3, 1, 2, 3]. It does NOT multiply each element (A) or create a nested list (C).MCQ 14
What is the result of list('hello')?
Answer: B
B is correct. The
B is correct. The
list() constructor converts any iterable into a list. A string is iterable (character by character), so list('hello') creates a list of individual characters: ['h', 'e', 'l', 'l', 'o'].MCQ 15
What is the output of nums = [1, 2]; nums += [3]; print(type(nums))?
Answer: B
B is correct.
B is correct.
+= with lists is equivalent to extend(). It adds the elements from [3] to nums. The result is still a list [1, 2, 3]. The type remains list.MCQ 16
What is the output of [[0]] * 3 after modifying the first inner list?
Answer: B
B is correct.
B is correct.
[[0]] * 3 creates three references to the SAME inner list object. Modifying any one of them modifies all of them because they are the same object. To create independent inner lists, use a list comprehension: [[0] for _ in range(3)].MCQ 17
Which method reverses a list in place?
Answer: B
B is correct.
B is correct.
.reverse() reverses the list in place and returns None. reversed() (A) returns an iterator, not a list, and does not modify the original. [::-1] (D) creates a new reversed list. .flip() (C) does not exist.MCQ 18
What does del nums[1:3] do to the list nums = [10, 20, 30, 40, 50]?
Answer: B
B is correct.
B is correct.
del nums[1:3] deletes the slice from index 1 to 2 (index 3 is excluded). Elements 20 and 30 are removed. The result is [10, 40, 50]. Slice notation always excludes the stop index.Coding Challenges
Challenge 1: Find the Second Largest
EasyGiven the list [45, 12, 78, 34, 56, 89, 23], write a program that finds and prints the second largest number without using sort() or sorted().
Sample Input
(No input required)
Sample Output
Second largest: 78
Use a loop. Do not sort the list.
nums = [45, 12, 78, 34, 56, 89, 23]
largest = max(nums[0], nums[1])
second = min(nums[0], nums[1])
for num in nums[2:]:
if num > largest:
second = largest
largest = num
elif num > second:
second = num
print("Second largest:", second)Challenge 2: Remove Duplicates
EasyAarav has the list [1, 2, 3, 2, 4, 3, 5, 1]. Write a program that removes duplicates while preserving the original order. Print the result.
Sample Input
(No input required)
Sample Output
[1, 2, 3, 4, 5]
Use a loop. Preserve order of first occurrence.
nums = [1, 2, 3, 2, 4, 3, 5, 1]
result = []
for num in nums:
if num not in result:
result.append(num)
print(result)Challenge 3: Flatten a 2D List
EasyGiven the 2D list [[1, 2, 3], [4, 5], [6, 7, 8, 9]], write a program that flattens it into a single list [1, 2, 3, 4, 5, 6, 7, 8, 9].
Sample Input
(No input required)
Sample Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Use nested loops or list comprehension.
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat = [num for row in matrix for num in row]
print(flat)Challenge 4: Rotate a List
MediumWrite a program that rotates the list [1, 2, 3, 4, 5] to the right by 2 positions. After rotation, the last 2 elements should move to the front.
Sample Input
(No input required)
Sample Output
[4, 5, 1, 2, 3]
Use slicing. Do not use any external library.
nums = [1, 2, 3, 4, 5]
k = 2
rotated = nums[-k:] + nums[:-k]
print(rotated)Challenge 5: Matrix Transpose
MediumPriya has a 3x3 matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write a program that transposes it (rows become columns). Print the result.
Sample Input
(No input required)
Sample Output
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Use nested loops or list comprehension.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transpose = [[matrix[j][i] for j in range(3)] for i in range(3)]
print(transpose)Challenge 6: Group by Even and Odd
MediumGiven the list [12, 7, 3, 18, 5, 22, 9, 14], write a program that separates it into two lists: one with even numbers and one with odd numbers. Print both.
Sample Input
(No input required)
Sample Output
Even: [12, 18, 22, 14]
Odd: [7, 3, 5, 9]
Use list comprehension.
nums = [12, 7, 3, 18, 5, 22, 9, 14]
evens = [x for x in nums if x % 2 == 0]
odds = [x for x in nums if x % 2 != 0]
print("Even:", evens)
print("Odd:", odds)Challenge 7: Find Common Elements
MediumRohan has two lists: [1, 2, 3, 4, 5] and [3, 4, 5, 6, 7]. Write a program that finds and prints the common elements in both lists, preserving order from the first list.
Sample Input
(No input required)
Sample Output
[3, 4, 5]
Use a loop or list comprehension. Do not use sets.
a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7]
common = [x for x in a if x in b]
print(common)Challenge 8: Frequency Count Without Dictionary
HardGiven the list [3, 1, 2, 3, 4, 2, 3, 1, 5], write a program that prints each unique element and its frequency. Do not use dictionaries.
Sample Input
(No input required)
Sample Output
3 appears 3 times
1 appears 2 times
2 appears 2 times
4 appears 1 times
5 appears 1 times
Use a loop and count(). Track already-counted elements.
nums = [3, 1, 2, 3, 4, 2, 3, 1, 5]
counted = []
for num in nums:
if num not in counted:
print(f"{num} appears {nums.count(num)} times")
counted.append(num)Challenge 9: Create a Safe 2D Matrix
HardWrite a program that creates a 3x4 matrix (3 rows, 4 columns) initialized with zeros. Then set the element at row 1, column 2 to 99. Print the matrix row by row. Make sure modifying one row does NOT affect others.
Sample Input
(No input required)
Sample Output
[0, 0, 0, 0]
[0, 0, 99, 0]
[0, 0, 0, 0]
Use list comprehension (not [[0]*4]*3). Verify independence of rows.
matrix = [[0 for _ in range(4)] for _ in range(3)]
matrix[1][2] = 99
for row in matrix:
print(row)Challenge 10: Merge Two Sorted Lists
HardGiven two sorted lists [1, 3, 5, 7] and [2, 4, 6, 8], write a program that merges them into a single sorted list without using sort() or sorted().
Sample Input
(No input required)
Sample Output
[1, 2, 3, 4, 5, 6, 7, 8]
Use two pointers (indices) to merge efficiently.
a = [1, 3, 5, 7]
b = [2, 4, 6, 8]
result = []
i = 0
j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
result.extend(a[i:])
result.extend(b[j:])
print(result)Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Python with a live mentor?
Explore our Python course