What is the difference between Python's list methods append and extend?

Submitted 3 years, 6 months ago
Ticket #246
Views 227
Language/Framework Python
Priority Low
Status Closed

What's the difference between the list methods append() and extend()?

Submitted on Oct 19, 20
add a comment

1 Answer

Verified

The append appends object at the end.

x = [1, 2, 3]
x.append([4, 5])
print (x)

gives you: [1, 2, 3, [4, 5]]

The extend extends list by appending elements from the iterable.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

gives you: [1, 2, 3, 4, 5]

 

Submitted 3 years, 6 months ago


Latest Blogs