- Python — Add List Items
- Example
- Insert Items
- Example
- Extend List
- Example
- Add Any Iterable
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- 4 Easy Ways to Add/Append List to List in Python
- Method 1: Using the list.extend() function
- Syntax
- Parameters
- Return Value
- Example
- Method 2: Using the list.append() function
- Syntax
- Parameters
- Example
- Method 3: Using list.insert() Method
- Example
- Method 4: Using list concatenation
- Example
- How To add Elements to a List in Python
- Prerequisites
- append()
- insert()
- extend()
- List Concatenation
- Conclusion
Python — Add List Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
Insert Items
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
Example
Insert an item as the second position:
Note: As a result of the examples above, the lists will now contain 4 items.
Extend List
To append elements from another list to the current list, use the extend() method.
Example
Add the elements of tropical to thislist :
thislist = [«apple», «banana», «cherry»]
tropical = [«mango», «pineapple», «papaya»]
thislist.extend(tropical)
print(thislist)
The elements will be added to the end of the list.
Add Any Iterable
The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).
Example
Add elements of a tuple to a list:
thislist = [«apple», «banana», «cherry»]
thistuple = («kiwi», «orange»)
thislist.extend(thistuple)
print(thislist)
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
4 Easy Ways to Add/Append List to List in Python
Here are 4 methods to add/append a list to a list in Python:
- Using list.extend() method
- Using list.append() method
- Using list.insert() method
- Using list concatenation
Method 1: Using the list.extend() function
The easiest way to append/add a list to a list in Python is to use the “list.extend()” method. The list.extend() is a built-in function that adds all the items of an iterable (list, tuple, string, etc.) to the end of the list.
Syntax
Parameters
iterable: The extend() method takes an iterable such as a list, tuple, string, etc.
Return Value
The extend() method modifies the original list. It doesn’t return any value.
Example
listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"]
Append the listB to a listA using the extend() function.
listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"] listA.extend(listB) print(listA)
['Millie', 'Caleb', 'Gaten', 'Finn', 'Noah', 'Sadie']
Method 2: Using the list.append() function
You can use the “list.append()” method to append a list into a list as an element. The list append() is a built-in function that adds a single element to the existing list.
Syntax
Parameters
The list.append() function takes a single item and adds it to the end of the list.
Example
Let’s take the above example, and instead of using extend() method, let’s use the append() function.
listA = ["Millie", "Caleb", "Gaten"] listB = ["Finn", "Noah", "Sadie"] listA.append(listB) print(listA)
['Millie', 'Caleb', 'Gaten', ['Finn', 'Noah', 'Sadie']]
Here you can see that the second list is added as a single element to the existing list.
Method 3: Using list.insert() Method
The insert() method inserts the list before the given index.
Example
# Define the initial list list1 = [1, 2, 3] # Define the list to be added list2 = [4, 5, 6] # Insert list2 into list1 at index 1 list1.insert(1, list2) print(list1)
Method 4: Using list concatenation
To concatenate a list in Python, you can use the “+” operator.
Example
# Define the initial list list1 = [1, 2, 3] # Define the list to be added list2 = [4, 5, 6] list3 = list1 + list2 print(list3)
How To add Elements to a List in Python
In this tutorial, we will learn different ways to add elements to a list in Python.
There are four methods to add elements to a List in Python.
- append() : append the element to the end of the list.
- insert() : inserts the element before the given index.
- extend() : extends the list by appending elements from the iterable.
- List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.
Prerequisites
In order to complete this tutorial, you will need:
This tutorial was tested with Python 3.9.6.
append()
This function adds a single element to the end of the list.
fruit_list = ["Apple", "Banana"] print(f'Current Fruits List fruit_list>') new_fruit = input("Please enter a fruit name:\n") fruit_list.append(new_fruit) print(f'Updated Fruits List fruit_list>')
Current Fruits List ['Apple', 'Banana'] Please enter a fruit name: Orange Updated Fruits List ['Apple', 'Banana', 'Orange']
This example added Orange to the end of the list.
insert()
This function adds an element at the given index of the list.
num_list = [1, 2, 3, 4, 5] print(f'Current Numbers List num_list>') num = int(input("Please enter a number to add to list:\n")) index = int(input(f'Please enter the index between 0 and len(num_list) - 1> to add the number:\n')) num_list.insert(index, num) print(f'Updated Numbers List num_list>')
Current Numbers List [1, 2, 3, 4, 5] Please enter a number to add to list: 20 Please enter the index between 0 and 4 to add the number: 2 Updated Numbers List [1, 2, 20, 3, 4, 5]
This example added 20 at the index of 2 . 20 has been inserted into the list at this index.
extend()
This function adds iterable elements to the list.
extend_list = [] extend_list.extend([1, 2]) # extending list elements print(extend_list) extend_list.extend((3, 4)) # extending tuple elements print(extend_list) extend_list.extend("ABC") # extending string elements print(extend_list)
[1, 2] [1, 2, 3, 4] [1, 2, 3, 4, 'A', 'B', 'C']
This example added a list of [1, 2] . Then it added a tuple of (3, 4) . And then it added a string of ABC .
List Concatenation
If you have to concatenate multiple lists, you can use the + operator. This will create a new list, and the original lists will remain unchanged.
evens = [2, 4, 6] odds = [1, 3, 5] nums = odds + evens print(nums) # [1, 3, 5, 2, 4, 6]
This example added the list of evens to the end of the list of odds . The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.
Conclusion
Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator
Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!