What will the lists l1 and l2 look like after performing l1.extend(l2) and l2.append(l1)?
💡 Model Answer
After performing l1.extend(l2) and l2.append(l1), l1 will be [1,2,3,4,5,6,7,8] and l2 will be [5,6,7,8,[1,2,3,4,5,6,7,8]]. The extend method concatenates the elements of l2 into l1, while append adds l1 as a single element to l2. This results in a nested list inside l2. If you want to avoid nesting, you could use l2.extend(l1) instead of l2.append(l1). Also note that l2.append(l1) stores a reference to l1, so changes to l1 will be reflected inside l2's nested list. In practice, you should choose extend when you want to merge two lists, and append when you want to add a single element, which could be another list, to the end of a list. This distinction is crucial for writing clear and bug‑free code.
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