Sign up for Free !!
World's first open source developer community with
Ticketing System
What's the difference between the list methods append() and extend()?
append()
extend()
The append appends object at the end.
The
append
x = [1, 2, 3] x.append([4, 5]) print (x)
gives you: [1, 2, 3, [4, 5]]
[1, 2, 3, [4, 5]]
The extend extends list by appending elements from the iterable.
extend
x = [1, 2, 3] x.extend([4, 5]) print (x)
gives you: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Please Login/Register to write your answer !!!
The
append
appends object at the end.gives you:
[1, 2, 3, [4, 5]]
The
extend
extends list by appending elements from the iterable.gives you:
[1, 2, 3, 4, 5]