Tuples, Sets & Dicts Questions with Answers

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

Sample questions

Question 1Easy

What is the output?

d = dict(zip("abc", range(3)))
print(d["c"], len(d))
  1. 2 2
  2. 2 3
  3. c 3
  4. 3 3

Answer: B. 2 3

Explanation

zip pairs a-0, b-1, c-2 and dict() builds a mapping from the pairs.

Step-by-step solution

  1. zip('abc', range(3)) pairs (a,0),(b,1),(c,2).
  2. dict builds {'a':0,'b':1,'c':2}.
  3. d['c'] is 2 and len is 3.

Why the other options are wrong

  • A. There are 3 keys.
  • C. d['c'] is the value 2, not 'c'.
  • D. range(3) values are 0,1,2.

Tip: dict(zip(k, v)).

Question 2Easy

What is the output?

a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
print(a | b)
  1. {'x': 1, 'y': 3, 'z': 4}
  2. {'x': 1, 'y': 2, 'z': 4}
  3. {'y': 3, 'z': 4, 'x': 1}
  4. TypeError

Answer: A. {'x': 1, 'y': 3, 'z': 4}

Explanation

The | merge operator (3.9+) keeps the left order and lets the right operand win on duplicate keys.

Step-by-step solution

  1. The | operator merges two dicts into a new dict.
  2. Keys from a come first: x and y, then z from b.
  3. The value for y comes from b, which wins on duplicate keys, so y is 3.
  4. The output is {'x': 1, 'y': 3, 'z': 4}.

Why the other options are wrong

  • B. The right dict wins on duplicates, so y is 3, not 2.
  • C. Left-side keys come first, so x is first in the output.
  • D. Dict union with | works from Python 3.9, so no TypeError is raised.

Tip: In a | b, b's values win on duplicate keys.

Question 3Easy

What is the output?

a, b = 1, 2
a, b = b, a + b
print(a, b)
  1. 2 2
  2. 2 3
  3. 2 4
  4. 1 3

Answer: B. 2 3

Explanation

The right-hand tuple (2, 3) is built from the old values before assignment.

Step-by-step solution

  1. Before the swap, a is 1 and b is 2.
  2. The right side builds the tuple (2, 3) from the old values: b is 2, and a plus b is 3.
  3. The tuple is then unpacked, so a becomes 2 and b becomes 3.
  4. The output is 2 3.

Why the other options are wrong

  • A. B is set to the old a plus b, which is 3, not 2.
  • C. B is the old a plus b, which is 3, not 4.
  • D. A takes the old b, which is 2, not 1.

Tip: The whole right side is evaluated before any name is assigned, so the tuple swap uses old values.

Question 4Easy

What is the output?

print(list({"a": 1, "b": 2}.items()))
  1. ['a', 1, 'b', 2]
  2. [['a', 1], ['b', 2]]
  3. [('a', 1), ('b', 2)]
  4. [('a', 'b'), (1, 2)]

Answer: C. [('a', 1), ('b', 2)]

Explanation

items() yields (key, value) tuples.

Step-by-step solution

  1. items() gives each key and value as a pair.
  2. List of that view gives a list of pairs.
  3. The pairs are tuples, so they print with round brackets.
  4. The output is [('a', 1), ('b', 2)].

Why the other options are wrong

  • A. Items gives pairs, not a flat list of keys and values.
  • B. Each pair is a tuple, not a list.
  • D. Each pair is (key, value), not transposed.

Tip: dict.items gives (key, value) tuples.

Question 5Medium

What is the output?

d = {}
print(d.setdefault("x", []), d.setdefault("x", [1]), d)
  1. [] [] {'x': []}
  2. [] [1] {'x': []}
  3. None [] {'x': []}
  4. [] [1] {'x': [1]}

Answer: A. [] [] {'x': []}

Explanation

setdefault inserts the default only when the key is missing and returns the stored value either way.

Step-by-step solution

  1. Setdefault for x inserts the empty list, since x is missing, and returns it.
  2. The second setdefault finds x already there, so it returns the stored empty list.
  3. The default [1] is never stored.
  4. The output is [] [] {'x': []}.

Why the other options are wrong

  • B. The first call returns the stored empty list, not [1].
  • C. Setdefault returns the stored value, not None.
  • D. The default [1] is ignored because x was already set, so d holds an empty list.

Tip: Setdefault stores the default only when the key is missing and always returns the stored value.

Question 6Medium

What is the output?

first, *mid, last = "python"
print(first, mid, last)
  1. ValueError
  2. p ['y', 't', 'h', 'o'] n
  3. p ytho n
  4. p ('y', 't', 'h', 'o') n

Answer: B. p ['y', 't', 'h', 'o'] n

Explanation

Any iterable can be unpacked; the starred name gets a list of the middle characters.

Step-by-step solution

  1. first takes 'p' and last takes 'n'.
  2. *mid takes the rest as a list.
  3. mid = ['y','t','h','o'].

Why the other options are wrong

  • A. Unpacking strings works.
  • C. mid is a list, printed with brackets.
  • D. The starred target is a list, not a tuple.

Tip: *name is always a list.

Question 7Medium

What is the output?

print((1, 2) + (3,), (0,) * 3, (1, 2) < (1, 3))
  1. (1, 2, (3,)) (0, 0, 0) True
  2. (1, 2, 3) (0, 0, 0) True
  3. (4, 2) (0, 0, 0) True
  4. (1, 2, 3) (0,) True

Answer: B. (1, 2, 3) (0, 0, 0) True

Explanation

Tuples support concatenation and repetition, and compare element by element.

Step-by-step solution

  1. The plus operator joins the tuples: (1, 2) plus (3,) gives (1, 2, 3).
  2. The star operator repeats the tuple: (0,) times 3 gives (0, 0, 0).
  3. Tuples compare item by item: 1 equals 1, then 2 is less than 3, so the test is True.
  4. The output is (1, 2, 3) (0, 0, 0) True.

Why the other options are wrong

  • A. The plus operator joins the items, so 3 is not nested in a tuple.
  • C. Tuple plus joins items and does not add them; (4, 2) is not a result here.
  • D. (0,) times 3 has three zeros, not one.

Tip: Tuples support plus to join, star to repeat, and item-by-item comparison.

Question 8Medium

What is the output?

d = {"a": 1}
k = d.keys()
d["b"] = 2
print(len(k), "b" in k)
  1. 1 True
  2. 1 False
  3. 2 False
  4. 2 True

Answer: D. 2 True

Explanation

keys() returns a live view, so it reflects keys added later.

Step-by-step solution

  1. K is a live view of the keys of d, not a copy.
  2. After d gets key b, the view includes it, so len of k is 2.
  3. B is now in the view, so the membership test is True.
  4. The output is 2 True.

Why the other options are wrong

  • A. The view shows both keys, so len is 2, not 1.
  • B. The view reflects the new key, so len is 2 and b is found.
  • C. The view is live, so the membership test for b is True.

Tip: dict.keys, values and items are live views that update when the dict changes.

Question 9Hard

What is the output?

d = {"a": 1, "b": 2}
try:
    for k in d:
        d[k + "x"] = 0
except RuntimeError as e:
    print("RuntimeError")
  1. KeyError
  2. RuntimeError
  3. {'a': 1, 'b': 2, 'ax': 0, 'bx': 0}
  4. Nothing is printed

Answer: B. RuntimeError

Explanation

Adding keys while iterating over a dict changes its size, which raises RuntimeError: dictionary changed size during iteration.

Step-by-step solution

  1. The loop adds new keys to d while iterating.
  2. Changing dict size during iteration raises RuntimeError.
  3. The handler prints RuntimeError.

Why the other options are wrong

  • A. d[k+'x'] assigns, it does not read, so no KeyError.
  • C. The error stops the loop, and nothing prints the dict.
  • D. The except handler prints the message.

Tip: Iterate over list(d) to be safe.

Question 10Hard

What is the output?

d = {(1, 2): "t", 1: "one"}
d[1.0] = "float"
print(len(d), d[(1, 2)], d[1])
  1. 3 t one
  2. 2 t float
  3. 3 t float
  4. TypeError

Answer: B. 2 t float

Explanation

Tuples of hashables are valid keys; 1.0 == 1 and they hash equally, so d[1.0] updates the existing key 1.

Step-by-step solution

  1. The keys are the tuple (1, 2) and the integer 1.
  2. 1.0 equals 1 and has the same hash, so d[1.0] updates the existing key 1 to float.
  3. So there are still two keys, and d[(1, 2)] is t, and d[1] is float.
  4. The output is 2 t float.

Why the other options are wrong

  • A. 1.0 matches the existing key 1, so the length stays 2 and the value is float.
  • C. The length is 2, since 1.0 does not add a key.
  • D. A float can be a dict key, so no TypeError is raised.

Tip: Keys that are equal, such as 1 and 1.0, refer to the same dict entry.

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