Python Add List Items (original) (raw)
Last Updated : 19 Apr, 2025
Python lists are **dynamic, which means we can add items to them **anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:
Add a Single Item Using append()
**append() method adds one item to the end of the list. It modifies the original list in-place.
Python `
li = [1, 2, 3] li.append(4) print(li)
`
**Explanation: **append() method is used to add the number 4 to the list li. After running the code, the list becomes [1, 2, 3, 4].
Add Multiple Items Using extend()
**extend() method adds each element from an iterable (like a list or tuple) to the end of the existing list.
Python `
li = [1, 2, 3] li.extend([4, 5, 6]) print(li)
`
**Explanation: **extend() method adds all items from the iterable (like a list) to the end of the original list. The result will be **[1, 2, 3, 4, 5, 6].
Adding Items at Specific Position Using insert()
**insert(index, item) method places the item at the specified position. Existing elements are shifted to the right. We can also use **insert() to add multiple items at once.
Python `
li = [1, 2, 3] li.insert(1, 10) print(li)
li = [1, 2, 3] for i in [4, 5, 6]: li.insert(1, i) # Inserts items at index 1 one by one print(li)
`
Output
[1, 10, 2, 3] [1, 6, 5, 4, 2, 3]
**Explanation:
- **insert(1, 10) adds **10 at the second position (index 1) of the list. After running the code, the list becomes [1, 10, 2, 3].
- In second part, we insert items from the list **[4, 5, 6] into the original list **li. The items are added at index 1 but because we are inserting one by one, the list is modified each time.
Combine Lists Using the + Operator
The ****+** **operator joins two or more lists and returns a new list without modifying the original ones.
Python `
a = [1, 2, 3] b = [4, 5, 6] c = a + b print(c)
`
**Explanation: The + operator combines the two lists a and b into a new list c. The result is [1, 2, 3, 4, 5, 6] and the original lists remain unchanged.
Adding Items in a Loop
We can use a loop (like for) to add items conditionally or repeatedly using append().
Python `
li = [] for i in range(5): li.append(i) print(li)
`
**Explanation: This loop appends values from 0 to 4 to the list li. After running the loop, the list li will contain [0, 1, 2, 3, 4].
Also read: **append(), **extend(), **insert()