Python list interview questions test whether you understand how lists store data, how they change in place, and which methods and patterns to use for common tasks such as sorting, copying, slicing and removing duplicates. Lists are the most used data structure in Python, so interviewers for fresher, data and backend roles ask about them in almost every round. Below are 30 Python list interview questions with short, accurate answers and code you can run yourself.
Key takeaways
- A Python list is an ordered, mutable collection that can hold items of different types and allows duplicates.
- append adds one item, extend adds each item from an iterable, and insert adds at a given index.
- list.sort changes the list in place and returns None, while sorted returns a new sorted list.
- Assigning a list to a new name does not copy it; use copy for a shallow copy and copy.deepcopy for nested lists.
- Avoid mutable default arguments such as def f(items=[]) because the same list is shared between calls.
Basic Python list interview questions
1. What is a list in Python?
A list is a built-in ordered collection that is mutable, allows duplicates and can store mixed data types. It is written with square brackets, for example [1, 'two', 3.0].
2. Are Python lists mutable?
Yes. You can change, add or remove items after the list is created without making a new list object.
nums = [1, 2, 3]
nums[0] = 10
print(nums) # [10, 2, 3]3. How is a list different from a tuple?
A list is mutable and uses square brackets. A tuple is immutable and uses parentheses. Tuples can be used as dictionary keys if all their items are hashable, while lists cannot be used as keys.
4. How are lists stored internally in CPython?
A CPython list is a dynamic array of references to objects. It over-allocates space, so appending is usually fast, and it resizes the underlying array when it runs out of room.
5. What is the time complexity of common list operations?
| Operation | Average time |
|---|---|
| Index access lst[i] | O(1) |
| append | O(1) amortised |
| pop() from the end | O(1) |
| insert or pop at the start | O(n) |
| x in lst | O(n) |
| sort | O(n log n) |
6. What is the difference between append and extend?
a = [1, 2]
a.append([3, 4]) # [1, 2, [3, 4]]
b = [1, 2]
b.extend([3, 4]) # [1, 2, 3, 4]append adds its argument as one item. extend loops over the iterable and adds each element.
7. How does insert work?
insert(index, item) places the item before the given index. If the index is larger than the length, the item goes at the end.
8. What is the difference between remove, pop and del?
- remove(x) deletes the first matching value and raises ValueError if it is missing.
- pop(i) removes and returns the item at index i, or the last item if no index is given.
- del lst[i] deletes by index or slice and returns nothing.
9. How do you find the index of an item?
Use lst.index(x). It returns the first position and raises ValueError if the item is not present, so check with x in lst first when unsure.
10. How do you count occurrences of a value?
marks = [80, 90, 80, 70]
print(marks.count(80)) # 2Slicing and indexing questions
11. Explain list slicing.
The syntax is lst[start:stop:step]. start is included, stop is excluded and step sets the gap. Slicing returns a new list.
nums = [0, 1, 2, 3, 4, 5]
print(nums[1:4]) # [1, 2, 3]
print(nums[::2]) # [0, 2, 4]
print(nums[-2:]) # [4, 5]12. How do you reverse a list?
lst.reverse() reverses in place and returns None. lst[::-1] and list(reversed(lst)) return a new reversed list.
13. What does negative indexing mean?
Negative indexes count from the end: lst[-1] is the last item, lst[-2] the second last.
14. What happens if you access an index that does not exist?
Python raises IndexError. Slicing, however, never raises IndexError; it simply returns whatever items exist in that range.
Sorting and copying questions
15. What is the difference between sort and sorted?
a = [3, 1, 2]
result = a.sort()
print(result) # None
print(a) # [1, 2, 3]
b = [3, 1, 2]
print(sorted(b)) # [1, 2, 3]
print(b) # [3, 1, 2]sort changes the original list. sorted works on any iterable and returns a new list.
16. How do you sort a list of dictionaries by a key?
students = [{'name': 'Asha', 'marks': 82},
{'name': 'Ravi', 'marks': 91}]
students.sort(key=lambda s: s['marks'], reverse=True)17. Is Python’s list sort stable?
Yes. Items with equal keys keep their original relative order, which lets you sort by several keys in multiple passes.
18. What happens with b = a for a list?
Both names point to the same list object. Changing b also changes a. Check with a is b, which returns True.
19. What is the difference between a shallow copy and a deep copy?
import copy
outer = [[1, 2], [3, 4]]
shallow = outer.copy()
deep = copy.deepcopy(outer)
outer[0].append(99)
print(shallow[0]) # [1, 2, 99]
print(deep[0]) # [1, 2]A shallow copy creates a new outer list but shares the inner objects. A deep copy duplicates nested objects too.
20. What is the output of [[0] * 3] * 3 after changing one item?
grid = [[0] * 3] * 3
grid[0][0] = 1
print(grid) # [[1, 0, 0], [1, 0, 0], [1, 0, 0]]All three rows are the same inner list. Create independent rows with [[0] * 3 for _ in range(3)].
Coding and pattern questions
21. What is a list comprehension?
squares = [n * n for n in range(10) if n % 2 == 0]
print(squares) # [0, 4, 16, 36, 64]It is a short way to build a list from an iterable with an optional condition. It is usually clearer and often faster than an equivalent loop with append.
22. How do you remove duplicates from a list while keeping order?
items = [3, 1, 3, 2, 1]
unique = list(dict.fromkeys(items))
print(unique) # [3, 1, 2]list(set(items)) also removes duplicates but does not keep the original order.
23. How do you flatten a nested list?
nested = [[1, 2], [3], [4, 5]]
flat = [x for row in nested for x in row]
print(flat) # [1, 2, 3, 4, 5]24. How do you find the second largest number in a list?
nums = [10, 40, 30, 40, 20]
unique = sorted(set(nums))
print(unique[-2] if len(unique) > 1 else None) # 3025. How do you loop with index and value together?
for i, name in enumerate(['Asha', 'Ravi'], start=1):
print(i, name)26. How do you combine two lists element by element?
names = ['Asha', 'Ravi']
marks = [82, 91]
print(list(zip(names, marks))) # [('Asha', 82), ('Ravi', 91)]27. Why should you not modify a list while looping over it?
Removing items during iteration shifts positions, so some items are skipped. Loop over a copy, or build a new list with a comprehension:
nums = [1, 2, 2, 3]
nums = [n for n in nums if n != 2]28. What is the problem with a mutable default argument?
def add_item(item, bucket=[]):
bucket.append(item)
return bucket
print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] same list reusedThe default list is created once when the function is defined. Use bucket=None and create a new list inside the function.
29. When should you use a deque instead of a list?
Use collections.deque when you add or remove items at both ends often, such as in a queue. deque.popleft is O(1), while list.pop(0) is O(n).
30. What is the difference between == and is for lists?
== checks whether two lists have equal contents. is checks whether both names point to the exact same object.
a = [1, 2]
b = [1, 2]
print(a == b) # True
print(a is b) # FalseHow to prepare for Python list questions
- Type and run every example above instead of only reading it.
- Practise explaining in-place changes versus new objects, because many tricky questions test exactly that.
- Solve small problems such as rotating a list, merging two sorted lists and finding missing numbers.
For wider preparation, go through our main Python interview questions and practise with Python programming examples with solutions. Understanding how Python runs your code, covered in why Python is an interpreted language, also helps in interviews.
Frequently asked questions
Why are Python lists mutable?
Lists are designed as general-purpose containers that grow and change, so Python lets you modify them in place. When you need a fixed sequence, use a tuple.
Can a Python list contain different data types?
Yes. A single list can hold numbers, strings, other lists and any other objects, although keeping one type per list usually makes code easier to read.
Is a list comprehension faster than a for loop?
For building a new list, a comprehension is usually somewhat faster and shorter than a loop with append. For complex logic, a normal loop can be clearer.
What is the maximum size of a Python list?
There is no fixed small limit; in practice it is limited by available memory and the platform’s maximum index size, which you can check with sys.maxsize.
Find more Python guides in our Python category.
