Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Add List Items


Add List Items

Use the append() function to add a new item to the list. When using the append() function, the new item adds at the end of the list.

Example
colors_list = ["Red", "Blue", "Green"]
colors_list.append("Purple")
print(colors_list)
['Red', 'Blue', 'Green', 'Purple']

In the above example, we add the color "Purple" to a list using the append() function. You can see the output "Purple" color added to the end of the list.


Add List Items Using Insert Function

The insert() function allows you to add a new item to the list.

Using the insert() function, you can add a new item at a specified position without removing any other items from the list.

Example
colors_list = ["Red", "Blue", "Green"]
colors_list.insert(2, "Purple")
print(colors_list)
['Red', 'Blue', 'Purple', 'Green']

In the above example, we add the color "Purple" to a list at position 2, using the insert() function. You can see that the color "Purple" adds to position 2, and the color "Green" moves to position 3.


Extend List

Use the extend() function to add a new list to the current list.

Example
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]

colors_list.extend(new_colors_list)
print(colors_list)
['Red', 'Blue', 'Green', 'Purple', 'Grey']

We created two different lists, and then we used the extend() function to extend colors_list to new_colors_list. Here, you can see that when we print colors_list, all items from new_colors_list are added to colors_list.


In addition, the extend() function also supports tuples, sets, dictionaries iterable data types.

Example
colors_list = ["Red", "Blue", "Green"]
colors_tuple = ("Purple", "Grey")

colors_list.extend(colors_tuple)
print(colors_list)
['Red', 'Blue', 'Green', 'Purple', 'Grey']

In the above example, we extend the tuple (colors_tuple) iterable data type to the declared list (colors_list) data type.


Add Multiple List Items

Add At Beginning

To add a new list at the beginning of the original list, set the end item index to 0 (the list index count starts at 0).

Example
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]

colors_list[:0] = new_colors_list
print(colors_list)
['Purple', 'Grey', 'Red', 'Blue', 'Green']

The above example sets the end item index position to 0 when adding a new list to the original list, resulting in the new list items appearing first in the original list.


Add At End

Set the start index count as the length of the original list when adding a new list at the end.

Example
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]

colors_list[len(colors_list):] = new_colors_list
print(colors_list)
['Red', 'Blue', 'Green', 'Purple', 'Grey']

The above example sets the start item index position to the length (len(colors_list)) of the original list when adding a new list to the original list, resulting in the new list items appearing at the end of the original list.


Add In Middle

To add a new list at the center of the original list, set the start and end item positions.

Example
colors_list = ["Red", "Blue", "Green"]
new_colors_list = ["Purple", "Grey"]

colors_list[1:1] = new_colors_list
print(colors_list)
['Red', 'Purple', 'Grey', 'Blue', 'Green']

In the above example, we set the start and end index item positions as 1, resulting in the new list items appearing in the middle of the original list.