Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. list.append() / list.extend() / list.insert()

list.append() / list.extend() / list.insert()

These are methods for adding elements to a list. You can append to the end, merge another list, or insert at any position.

Syntax

# Adds a single element to the end of the list (returns None).
list.append(element)

# Adds all elements from another iterable to the end of the list (returns None).
list.extend(iterable)

# Inserts an element at the specified index position (returns None).
list.insert(index, element)

Method List

MethodDescription
list.append(x)Adds the element x as a single item to the end of the list. If you pass a list, it is added as a nested list.
list.extend(iterable)Adds each element of an iterable (list, tuple, string, etc.) to the end of the list. The original list is modified in place.
list.insert(i, x)Inserts element x at index position i. Existing elements shift to the right. If i is greater than or equal to len(list), the element is appended to the end.

Sample Code

# Use append() to add an element to the end of the list.
fruits = ["apple", "orange"]
fruits.append("grape")
print(fruits)  # ['apple', 'orange', 'grape']

# append() adds a list as a single nested element (watch out for nesting).
more = ["strawberry", "peach"]
fruits.append(more)  # The entire list is added as one element.
print(fruits)  # ['apple', 'orange', 'grape', ['strawberry', 'peach']]
print(len(fruits))  # 4 (four elements total)

# Use extend() to add elements from another list individually.
fruits2 = ["apple", "orange"]
more2 = ["strawberry", "peach"]
fruits2.extend(more2)
print(fruits2)  # ['apple', 'orange', 'strawberry', 'peach']
print(len(fruits2))  # 4

# Passing a string to extend() adds it one character at a time.
letters = ["a", "b"]
letters.extend("cde")  # A string is treated as an iterable.
print(letters)  # ['a', 'b', 'c', 'd', 'e']

# Use insert() to add an element at the beginning (index 0).
nums = [2, 3, 4]
nums.insert(0, 1)
print(nums)  # [1, 2, 3, 4]

# Use insert() to add an element in the middle.
nums.insert(2, 99)
print(nums)  # [1, 2, 99, 3, 4]

# A typical pattern: building a list by appending in a loop.
squares = []
for i in range(1, 6):
    squares.append(i ** 2)
print(squares)  # [1, 4, 9, 16, 25]

# The difference between the + operator and extend().
# + returns a new list (the original is unchanged).
a = [1, 2, 3]
b = a + [4, 5]
print(a)  # [1, 2, 3] (unchanged)
print(b)  # [1, 2, 3, 4, 5] (new list)

# extend() modifies the original list in place.
a.extend([4, 5])
print(a)  # [1, 2, 3, 4, 5]

Notes

Although append() and extend() look similar, they handle their arguments differently. When you pass a list, append() nests the entire list as a single element, whereas extend() unpacks the list and adds each element individually. Use extend() when you want to merge the elements of one list into another.

insert() is useful when you need to add an element at the beginning (index 0) or somewhere in the middle of a list. Keep in mind that inserting at the beginning requires shifting all existing elements, so it can be costly for large lists. If you frequently add to the front, consider using collections.deque and its appendleft() method instead.

All three methods return None. Writing new_list = my_list.append(x) assigns None to new_list, not the updated list. For removing elements, see list.pop() / list.remove().

If you find any errors or copyright issues, please .