Home › Interview Questions › What will be printed when you perform l1.extend(l2…

What will be printed when you perform l1.extend(l2) and l2.append(l1) and then print l1 and l2?

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

💡 Model Answer

l1.extend(l2) takes each element from l2 and appends it to l1, so l1 becomes [1,2,3,4,5,6,7,8]. l2.append(l1) adds the entire l1 list as a single element to l2, resulting in l2 = [5,6,7,8,[1,2,3,4,5,6,7,8]]. When you print l1 you see the flattened list, and printing l2 shows the nested list at the end. The difference between extend and append is that extend iterates over the argument and adds each item individually, while append adds the argument as one object. This behavior is consistent across Python lists and is useful when you want to merge lists or add a sublist as a single element. Because l2.append(l1) stores a reference to the same list object, any later changes to l1 will be reflected inside l2's nested list. If you want to preserve the original state of l1 inside l2, you should use a shallow copy or a deep copy depending on the nested structure. This subtlety is often a source of bugs when working with mutable objects 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