Lists & Slicing Questions with Answers

31 practice questions on Lists & Slicing (Python) with answers and step-by-step solutions. Try 10 samples below, then practise all 31 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

a = [1, 2, 3, 2]
print(a.count(2), len(a), a.reverse())
  1. 2 3 None
  2. 2 4 None
  3. 1 4 None
  4. 2 4 [2, 3, 2, 1]

Answer: B. 2 4 None

Explanation

reverse() reverses in place and returns None, like most mutating list methods.

Step-by-step solution

  1. Count of 2 in 1, 2, 3, 2 is 2.
  2. Len of the list is 4.
  3. Reverse reverses the list in place and returns None, which is printed as None.
  4. The output is 2 4 None.

Why the other options are wrong

  • A. The list has four items, so len is 4, not 3.
  • C. 2 appears twice, so count is 2, not 1.
  • D. Reverse returns None, not the list.

Tip: Most mutating list methods such as reverse, sort and append return None.

Question 2Easy

What is the output?

a = [5, 3, 5, 1]
a.remove(5)
print(a, a.index(5))
  1. [5, 3, 1] 2
  2. [3, 5, 1] 2
  3. [3, 1] -1
  4. [3, 5, 1] 1

Answer: D. [3, 5, 1] 1

Explanation

remove deletes only the first matching value; index then finds the remaining 5 at position 1.

Step-by-step solution

  1. Remove deletes only the first 5, leaving 3, 5, 1.
  2. index(5) finds the remaining 5 at position 1.
  3. The printed list is [3, 5, 1] and the index is 1.
  4. The output is [3, 5, 1] 1.

Why the other options are wrong

  • A. Remove deletes only the first 5, so [5, 3, 1] is not the list.
  • B. The remaining 5 is at index 1, not 2.
  • C. Remove deletes one 5 only, so the other 5 is still in the list.

Tip: Remove deletes only the first matching value; index raises an error if the value is missing.

Question 3Easy

What does a[-1] return for a non-empty list a?

  1. The last element
  2. The second-last element
  3. IndexError always
  4. The first element

Answer: A. The last element

Explanation

Negative indices count from the end, with -1 being the last element.

Step-by-step solution

  1. Negative index counts from the end.
  2. -1 is the last element.

Why the other options are wrong

  • B. -2 would be second-last.
  • C. Non-empty lists always allow -1.
  • D. 0 is the first.

Tip: -1 = last.

Question 4Easy

What is the output?

a = [1, 2]
a.append([3, 4])
b = [1, 2]
b.extend([3, 4])
print(len(a), len(b))
  1. 4 4
  2. 4 3
  3. 3 4
  4. 3 3

Answer: C. 3 4

Explanation

append adds the list as one element; extend adds each element separately.

Step-by-step solution

  1. Append adds the whole list [3, 4] as one element, so a has 3 items.
  2. Extend adds 3 and 4 one by one, so b has 4 items.
  3. Len of a is 3 and len of b is 4.
  4. The output is 3 4.

Why the other options are wrong

  • A. Append adds one element, so a has length 3, not 4.
  • B. Extend adds two items, so b has length 4, not 3.
  • D. Extend adds two items, so b has length 4, not 3.

Tip: Append adds one object; extend adds each item of an iterable.

Question 5Medium

What is the output?

a = list(range(10))
print(a[2:8:3], a[-3::-3])
  1. [3, 6] [7, 4, 1]
  2. [2, 5, 8] [7, 4, 1]
  3. [2, 5] [7, 4, 1, -2]
  4. [2, 5] [7, 4, 1]

Answer: D. [2, 5] [7, 4, 1]

Explanation

Indices 2 and 5 are taken (8 is excluded); from index -3 (value 7) stepping back by 3 gives 7, 4, 1.

Step-by-step solution

  1. a[2:8:3] starts at index 2 and steps by 3, taking indices 2 and 5; 8 is excluded.
  2. That gives 2 and 5.
  3. a[-3::-3] starts at index 7, the third from the end, then goes back by 3: 7, 4, 1.
  4. The output is [2, 5] [7, 4, 1].

Why the other options are wrong

  • A. The slice starts at index 2, so 2 and 5 are right and 3 and 6 are not.
  • B. The stop of 8 is excluded, so 8 is not in the result.
  • C. The backward step stops before index 0, so -2 is not included.

Tip: The stop index is never included in a slice; with a negative step, the walk stops before the stop index.

Question 6Medium

What is the time complexity of list.insert(0, x) for a Python list of n elements?

  1. O(log n)
  2. O(n), because every existing element shifts right
  3. O(1), because lists are linked lists
  4. O(1) amortised, like append

Answer: B. O(n), because every existing element shifts right

Explanation

A Python list is a dynamic array; inserting at the front moves all n elements. Use collections.deque for O(1) at both ends.

Step-by-step solution

  1. A Python list is a dynamic array.
  2. Inserting at index 0 shifts all n elements right.
  3. That is O(n).

Why the other options are wrong

  • A. There is no binary search involved.
  • C. Lists are not linked lists.
  • D. append is amortised O(1), insert(0) is not.

Tip: Use deque for fast front inserts.

Question 7Medium

What is the output?

print(sorted([3, -1, -4, 2], key=abs), max([3, -5, 2], key=abs))
  1. [-1, 2, 3, -4] 5
  2. [-1, 2, -4, 3] -5
  3. [-1, 2, 3, -4] -5
  4. [-4, -1, 2, 3] 3

Answer: C. [-1, 2, 3, -4] -5

Explanation

key=abs orders by absolute value but returns the original elements.

Step-by-step solution

  1. Sorted with key abs orders by absolute value: 1, 2, 3, 4.
  2. So the items come out as -1, 2, 3, -4, keeping their signs.
  3. Max with key abs picks the element with the largest absolute value, which is -5.
  4. The output is [-1, 2, 3, -4] -5.

Why the other options are wrong

  • A. Max with key abs returns the original element -5, not its absolute value 5.
  • B. -4 has absolute value 4, so it comes after 3, not before it.
  • D. -4 has absolute value 4, the largest, so it comes last; max is -5.

Tip: Key abs sorts by size without the sign; the function returns the original values.

Question 8Medium

What is the output?

a = [1, 2]
a.extend("hi")
print(a)
  1. TypeError
  2. [1, 2, 'h', 'i']
  3. [1, 2, 'hi']
  4. [1, 2, 'h', 'i', '']

Answer: B. [1, 2, 'h', 'i']

Explanation

extend iterates over its argument, and iterating a string yields its characters.

Step-by-step solution

  1. Extend iterates over its argument.
  2. Iterating a string gives each character, h and i.
  3. So h and i are added as separate items after 1 and 2.
  4. The output is [1, 2, 'h', 'i'].

Why the other options are wrong

  • A. Extend accepts any iterable, and a string is iterable, so there is no error.
  • C. Extend adds each character separately, so hi is not added as one item.
  • D. No empty string is added; only the two characters are appended.

Tip: Extend takes each item of an iterable, and a string yields single characters.

Question 9Hard

What is the output?

a = [1, 2, 3]
a[len(a):] = [4]
a[:0] = [0]
print(a, a[-1:-1])
  1. [0, 1, 2, 3, 4] [4]
  2. [1, 2, 3, 4, 0] []
  3. [0, 1, 2, 3, 4] [3]
  4. [0, 1, 2, 3, 4] []

Answer: D. [0, 1, 2, 3, 4] []

Explanation

a[len(a):] = [...] appends and a[:0] = [...] prepends; a slice whose start equals its stop is empty.

Step-by-step solution

  1. a[len(a):] is an empty slice at the end, so assigning [4] appends it: [1, 2, 3, 4].
  2. a[:0] is an empty slice at the front, so assigning [0] prepends it: [0, 1, 2, 3, 4].
  3. a[-1:-1] starts and stops at the same index, so it is empty.
  4. The output is [0, 1, 2, 3, 4] [].

Why the other options are wrong

  • A. a[-1:-1] is empty, not [4].
  • B. Assigning to a[:0] puts 0 at the front, not the end.
  • C. a[-1:-1] is empty, not [3].

Tip: A slice whose start equals its stop is always empty, and assigning to an empty slice inserts there.

Question 10Hard

What is the output?

a = [1, 2, 2, 3]
for x in a:
    if x == 2:
        a.remove(x)
print(a)
  1. [1, 3]
  2. [1, 2, 3]
  3. RuntimeError
  4. [1, 2, 2, 3]

Answer: B. [1, 2, 3]

Explanation

After the first 2 is removed the second 2 shifts into the slot already visited, so the loop skips it and only one 2 is removed.

Step-by-step solution

  1. The loop starts on 1, then finds the first 2 and removes it, giving 1, 2, 3.
  2. The list shifts, so the second 2 moves into the slot the loop already passed.
  3. The loop moves on to 3 and never sees the second 2.
  4. The output is [1, 2, 3].

Why the other options are wrong

  • A. The second 2 is skipped, so only one 2 is removed, leaving a 2 in the list.
  • C. Python does not raise an error when a list changes during iteration.
  • D. The first 2 is removed, so the output cannot still have two 2s.

Tip: Do not remove items from a list while looping over it; build a new list instead.

Practise all 31 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics