Junior — Middle
What is the purpose of the extend method in Python lists?
sobes.tech AI
Answer from AI
The extend method in Python lists is used to add all elements from an iterable (like another list, tuple, or set) to the end of the current list. Unlike append, which adds a single element, extend expands the list by adding each element separately.
Example:
lst = [1, 2, 3]
lst.extend([4, 5])
print(lst) # Output: [1, 2, 3, 4, 5]