Lists, Tuples, Sets & Dicts Questions with Answers

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

Sample questions

Question 1Easy

What is the output?

print(sorted({3, 1, 2} | {2, 4}))
  1. [2]
  2. {1, 2, 3, 4}
  3. [1, 2, 2, 3, 4]
  4. [1, 2, 3, 4]

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

Explanation

| is set union, which removes duplicates: {1, 2, 3, 4}. sorted() returns a list.

Step-by-step solution

  1. The | sign between two sets is union: every value appears once, giving {1, 2, 3, 4}.
  2. sorted() turns any collection into a new list in increasing order.
  3. Sorted gives [1, 2, 3, 4], the correct answer.

Why the other options are wrong

  • A. [2] is wrong; | is union, not intersection, so all four values stay.
  • B. {1, 2, 3, 4} is a set, but sorted() always returns a list.
  • C. [1, 2, 2, 3, 4] is wrong; a set keeps each value only once, so 2 appears once.

Tip: | on sets is union and & is intersection. sorted() always returns a list.

Question 2Easy

What is the output?

print(type((5)).__name__, type((5,)).__name__)
  1. tuple tuple
  2. int tuple
  3. int int
  4. tuple int

Answer: B. int tuple

Explanation

Parentheses alone just group an expression, so (5) is the int 5. The trailing comma is what makes (5,) a one-element tuple.

Step-by-step solution

  1. (5) only groups the expression, so it is the int 5.
  2. (5,) has a trailing comma, which makes it a one-item tuple.
  3. So the output is int tuple, the correct answer.

Why the other options are wrong

  • A. Tuple tuple is wrong; (5) is an int, not a tuple.
  • C. Int int is wrong; (5,) is a tuple.
  • D. Tuple int is wrong; the types are in the wrong order and (5) is not a tuple.

Tip: A one-item tuple needs a trailing comma, as in (5,).

Question 3Easy

Which built-in type gives average O(1) time for membership tests (x in collection)?

  1. set
  2. str
  3. tuple
  4. list

Answer: A. set

Explanation

Sets are hash tables, so membership checks are O(1) on average. Lists, tuples and strings need a linear scan.

Step-by-step solution

  1. Sets are hash tables, so membership checks hash the item.
  2. Average cost is O(1).
  3. str, tuple and list scan items one by one: O(n).

Why the other options are wrong

  • B. Strings search sequentially, O(n).
  • C. Tuples are scanned linearly.
  • D. Lists are scanned linearly.

Tip: Need fast 'in' checks? Use a set.

Question 4Medium

What is the output?

d = {"a": 1}
print(d.get("b", 0), len(d))
  1. None 1
  2. KeyError
  3. 0 2
  4. 0 1

Answer: D. 0 1

Explanation

dict.get returns the default (0) when the key is missing and does not insert the key, so the length stays 1.

Step-by-step solution

  1. d.get("b", 0) looks for key b, which is missing, so it returns the default 0.
  2. Get does not add the key, so d still has only one key.
  3. len(d) is 1, so the output is 0 1, the correct answer.

Why the other options are wrong

  • A. None 1 is wrong; the default given is 0, so get returns 0.
  • B. KeyError is wrong; get returns the default instead of raising an error.
  • C. 0 2 is wrong; get does not add the key b, so the length stays 1.

Tip: dict.get(key, default) is a safe lookup: it never raises KeyError and does not add the key.

Question 5Medium

What is the output?

lst = [3, 1, 2]
print(lst.sort())
  1. [1, 2, 3]
  2. None
  3. Error
  4. [3, 1, 2]

Answer: B. None

Explanation

list.sort() sorts in place and returns None. Use sorted(lst) to get a new sorted list.

Step-by-step solution

  1. lst.sort() sorts the list in place, so lst becomes [1, 2, 3].
  2. The sort() method returns None, not the list.
  3. So print shows None, the correct answer.

Why the other options are wrong

  • A. [1, 2, 3] is wrong; lst is sorted, but sort() returns None.
  • C. Error is wrong; sort() is a normal list method and runs without error.
  • D. [3, 1, 2] is wrong; the list is changed in place, and sort() returns None.

Tip: list.sort() changes the list and returns None. sorted(lst) returns a new sorted list.

Question 6Medium

What is the output?

a = [1, 2, 3]
print(a.pop(), a.pop(0), a)
  1. 3 1 [1, 2, 3]
  2. 3 2 [1]
  3. 1 3 [2]
  4. 3 1 [2]

Answer: D. 3 1 [2]

Explanation

pop() with no argument removes the last item (3).

pop(0) removes the first (1).

Arguments are evaluated left to right, so a is printed as [2].

Step-by-step solution

  1. Arguments are evaluated left to right. a.pop() removes the last item, 3, so a is [1, 2].
  2. a.pop(0) removes the first item, 1, so a is [2].
  3. A is printed after both pops, as [2], so the output is 3 1 [2], the correct answer.

Why the other options are wrong

  • A. 3 1 [1, 2, 3] is wrong; the 3 and the 1 were removed from the list.
  • B. 3 2 [1] is wrong; pop(0) removes 1, not 2.
  • C. 1 3 [2] is wrong; pop() with no index removes the last item, 3, first.

Tip: pop() removes the last item, and pop(i) removes the item at index i. Arguments are evaluated left to right.

Question 7Hard

What is the output?

grid = [[0] * 2] * 2
grid[0][0] = 1
print(grid)
  1. [[1, 0], [1, 0]]
  2. [[1, 1], [1, 1]]
  3. [[1, 0], [0, 0]]
  4. [[1, 1], [0, 0]]

Answer: A. [[1, 0], [1, 0]]

Explanation

[[0] * 2] * 2 repeats a reference to the same inner list, so both rows are one object. Changing grid[0][0] shows up in both rows. Use [[0] * 2 for _ in range(2)] instead.

Step-by-step solution

  1. [0] * 2 makes a list [0, 0]; the outer * 2 repeats the same inner list object twice.
  2. grid[0] and grid[1] are the same list.
  3. Setting grid[0][0] = 1 changes both rows: [[1, 0], [1, 0]].

Why the other options are wrong

  • B. Only index 0 of the shared row changes.
  • C. That would need two separate rows.
  • D. Rows are identical, so this cannot happen.

Tip: Use [[0]*2 for _ in range(2)] for independent rows.

Question 8Hard

What is the output?

d = {1: "a", True: "b", 1.0: "c"}
print(d)
  1. {1: 'a', True: 'b', 1.0: 'c'}
  2. {1: 'c'}
  3. {1.0: 'c'}
  4. {True: 'c'}

Answer: B. {1: 'c'}

Explanation

1, True and 1.0 are equal and have the same hash, so they are the same key. The first key object (1) is kept and each later assignment overwrites the value, leaving {1: 'c'}.

Step-by-step solution

  1. 1, True and 1.0 are equal and have the same hash, so Python treats them as one key.
  2. The first key, 1, is kept. Each later assignment updates its value, so the value becomes c.
  3. The printed dict is {1: 'c'}, the correct answer.

Why the other options are wrong

  • A. Three keys are wrong; the three keys are equal, so only one key stays.
  • C. {1.0: 'c'} is wrong; the first key, 1, is kept, not 1.0.
  • D. {True: 'c'} is wrong; the first key, 1, is kept, not True.

Tip: In Python, True equals 1 and 1.0 equals 1, so they act as the same dict key.

Practise all 8 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