Home › Interview Questions › What will be the output of the following Python co…

What will be the output of the following Python code, and why?

🟡 Medium Conceptual Junior level
1Times asked
Mar 2026Last seen
Mar 2026First seen

💡 Model Answer

The code will print:

[1]

[1, 2]

The function uses a mutable default argument (a list). The default list is created once when the function is defined, not each time it is called. Thus, the first call appends 1 to the shared list and returns [1]. The second call appends 2 to the same list, so the list now contains [1, 2]. This is a common Python pitfall. The correct pattern is to use None as the default and create a new list inside the function: def append_item(item, lst=None): if lst is None: lst = [] ...

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