What's the difference between the list methods append()
and extend()
?
append() adds a single element to the end of the list while . extend() can add multiple individual elements to the end of the list.
What is the difference between the list methods append and extend? append adds its argument as a single element to the end of a list. The length of the list itself will increase by one. extend iterates over its argument adding each element to the list, extending the list.
append
appends object at the end.
>>> x = [1, 2, 3] >>> x.append([4, 5]) >>> print(x) [1, 2, 3, [4, 5]]
extend
extends list by appending elements from the iterable.
>>> x = [1, 2, 3] >>> x.extend([4, 5]) >>> print(x) [1, 2, 3, 4, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With