Skip to content
C

Python Interview Questions

Lists, Tuples, Sets & Dictionaries Interview Questions

Lists, tuples, sets, dictionaries, comprehensions, slicing, unpacking, and the built-in methods that manipulate them.

Question 1: What is a list?

Ans

A list is an ordered, mutable sequence that can contain objects of different types. It supports indexing, slicing, insertion, deletion, and iteration.

Example

python
numbers = [10, 20, 30] numbers.append(40) print(numbers[1])

Important Point

Lists allow duplicate values and are zero-indexed.

Question 2: What is a tuple?

Ans

A tuple is an ordered, immutable sequence. It is useful for fixed groups of values and can be used as a dictionary key when all contained values are hashable.

Example

python
point = (10, 20) x, y = point print(x, y)

Important Point

Tuple immutability does not recursively make contained mutable objects immutable.

Question 3: List vs tuple?

Ans

Lists are mutable and are suited to collections that change. Tuples are immutable and are useful for fixed records or values that should not be reassigned.

Example

python
items = [1, 2] items.append(3) point = (10, 20)

Important Point

Choose based on semantics, not on a blanket claim that tuples are always faster.

Question 4: What is a set?

Ans

A set is an unordered collection of unique hashable elements. It supports efficient average-case membership testing and mathematical set operations.

Example

python
skills = {"Java", "Python", "SQL"} skills.add("Git") print("Python" in skills)

Important Point

Set elements must be hashable.

Question 5: What is a frozenset?

Ans

A frozenset is an immutable set. Because it is immutable and hashable when its elements are hashable, it can itself be used as a dictionary key or set element.

Example

python
permissions = frozenset({"read", "write"}) print("read" in permissions)

Important Point

You cannot add or remove elements from a frozenset.

Question 6: What is a dictionary?

Ans

A dictionary stores key-value pairs and provides average-case constant-time lookup for hashable keys under normal conditions.

Example

python
student = {101: "Rahul", 102: "Amit"} print(student[101])

Important Point

Keys must be hashable and unique; assigning an existing key replaces its value.

Question 7: Can dictionary keys be lists?

Ans

No. Lists are mutable and unhashable, so they cannot be dictionary keys. Immutable hashable alternatives such as tuples can be used when their elements are hashable.

Example

python
locations = {(18.52, 73.85): "Pune"} print(locations[(18.52, 73.85)])

Important Point

A tuple containing an unhashable element is also unhashable.

Question 8: What is dictionary comprehension?

Ans

Dictionary comprehension creates a dictionary from an iterable using an expression and optional condition.

Example

python
squares = {n: n*n for n in range(1, 4)} print(squares)

Important Point

Avoid complicated comprehensions when a normal loop is easier to read.

Question 9: What is list comprehension?

Ans

List comprehension is a concise way to build a list by transforming or filtering values from an iterable.

Example

python
squares = [n*n for n in range(5) if n % 2 == 0] print(squares)

Important Point

A comprehension creates the output collection immediately; use a generator expression when lazy evaluation is preferred.

Question 10: What is slicing?

Ans

Slicing extracts part of a sequence using start, stop, and step positions. The stop index is excluded.

Example

python
s = "Python" print(s[1:4]) print(s[::-1])

Important Point

Negative indexes count from the end, and a negative step can traverse backwards.

Question 11: What is unpacking?

Ans

Unpacking assigns elements of an iterable to multiple names in one operation.

Example

python
name, age = ("Amit", 25) print(name, age)

Important Point

The number of targets must normally match the iterable length unless starred unpacking is used.

Question 12: What is starred unpacking?

Ans

A starred target collects remaining values into a list during assignment or expands an iterable when calling or constructing values.

Example

python
first, *middle, last = [1, 2, 3, 4] print(first, middle, last)

Important Point

Only one starred target is allowed in a single assignment target list.

Question 13: append() vs extend()?

Ans

append() adds one object as a single element. extend() iterates over another iterable and adds its elements individually.

Example

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

Important Point

This difference is a frequent list interview question.

Question 14: remove() vs pop() vs del?

Ans

remove(x) deletes the first matching value, pop(index) removes and returns an item, and del removes an item, slice, or name binding.

Example

python
a = [10, 20, 30] a.remove(20) x = a.pop() del a[0] print(x, a)

Important Point

`remove()` raises ValueError when the value is absent; `pop()` can raise IndexError.

Question 15: sort() vs sorted()?

Ans

list.sort() sorts a list in place and returns None. sorted() accepts any iterable and returns a new list without modifying the original iterable.

Example

python
a = [3, 1, 2] b = sorted(a) print(a, b) a.sort() print(a)

Important Point

Do not write `a = a.sort()` expecting the sorted list; that assigns None.

Continue Your Preparation