Python List Methods — Python Technical Interview Questions & Answers
Python List Methods
Python List Methods
- Most list methods change the same list. append(x) adds one item at the end. extend(iterable) adds many. insert(i, x) puts x at index i. remove(value) deletes the first match. pop() takes the last (or pop(i) that index). clear() empties the list. sort() and reverse() edit in place.
- append vs extend is the viva. names.append(["Neha"]) nests a list. names.extend(["Neha"]) adds the string Neha. sorted(names) returns a new list; names.sort() changes names. count(x) and index(x) only read. copy() is a shallow copy.
- Python List Methods — output: [70, 80, 90] then 90. sort is in place. pop removes last.
index → 0 1 2
list → [Asha, Ravi, Neha]Technical Interview Tip: append([1,2]) vs extend([1,2]).
Code Examples for Python List Methods
Example
# List methods
marks = [70, 90, 80]
marks.sort()
print(marks)
print(marks.pop())
Python List Methods — output: [70, 80, 90] then 90. sort is in place. pop removes last.
How to Explain Python List Methods to an Interviewer
Explain List Methods as if you are teaching a junior — definition, then one tiny script.
Most methods change the list. append vs extend.
What does the example print, and why?
Python List Methods — output: [70, 80, 90] then 90
What mistake do freshers make with List Methods?
sort vs sorted.