Python List append() and extend()
Core idea
items.append(value) adds one item at the end, even if the value is another list. items.extend(iterable) adds each item supplied by an iterable (something you can loop over, such as a list or string). Both modify the original list and return None.
B becomes the second item.
Compare the two methods
first = [1]second = [1]first.append([2, 3])second.extend([2, 3])print(first)print(second)append creates a nested list item; extend adds two separate integers.
Extend with text
codes = []result = codes.extend("AB")print(codes)print(result)A string supplies individual characters. The return value is None, not the updated list.
Common mistake
Avoid items = items.append(value). The method changes the list but assigning its return value makes the name refer to None.
Practice
Start with [10], append 20 and extend with [30, 40]. Predict the result if you use append for the last operation instead.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Append and extend
Check your understanding of append and extend.
What is printed?
items = [1]items.append([2, 3])print(items)What is printed?
items = []items.extend("AB")print(items)What is printed?
items = [1]print(items.append(2))To add three separate items from a list of three items, which method fits directly?