Home › Interview Questions › What will be the output of printing l1 and l2 afte…

What will be the output of printing l1 and l2 after performing l1.extend(l2) and l2.append(l1)?

🟢 Easy Conceptual Fresher level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

When you call l1.extend(l2), l1 becomes [1,2,3,4,5,6,7,8]. Then l2.append(l1) adds l1 as a nested list, so l2 becomes [5,6,7,8,[1,2,3,4,5,6,7,8]]. Printing l1 prints the flattened list, printing l2 prints the nested list. The difference between extend and append is that extend iterates over the argument and adds each element individually, while append adds the argument as a single element. This is a common source of confusion. If you want to avoid nested lists, use l2.extend(l1) instead of l2.append(l1). Additionally, because l2.append(l1) stores a reference to l1, any subsequent modifications to l1 will also appear inside l2's nested list. If you need a separate copy, use l2.append(l1.copy()). Understanding these nuances helps prevent bugs when manipulating lists in Python.

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