gotchapythonCriticalCanonical
What is the difference between Python's list methods append and extend?
Viewed 0 times
andmethodsbetweenextendthedifferenceappendlistwhatpython
Problem
What's the difference between the list methods
append() and extend()?Solution
.append() appends a single object at the end of the list:>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]].extend() appends multiple objects that are taken from inside the specified iterable:>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]Code Snippets
>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]Context
Stack Overflow Q#252703, score: 5929
Revisions (0)
No revisions yet.