What is the output of the following Python code that demonstrates list mutability?
π‘ Model Answer
The code creates a list a containing [1, 2, 3] and then passes it to a function that assigns the parameter b to the same list object. Inside the function, b.pop() removes the last element (1) from the list. Because b and a reference the same object, the modification is visible outside the function. The function returns the modified list, which is stored in c. When the program prints a and c, both show the updated list [2, 3]. Thus the output is:
[2, 3]
[2, 3]
This illustrates that lists are mutable and passed by reference in Python. The time complexity of pop() on a list is O(1) when removing the last element, and the overall function runs in O(1) time.
This answer was generated by AI for study purposes. Use it as a starting point β personalize it with your own experience.
π€ Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI β Starts at βΉ500