How to Add Elements to a List in Python (append, extend and insert)

By 

Published on

3 min read

Add Elements to a List in Python

When working with lists in Python, you will often want to add new elements to the list.

The Python list data type has three methods for adding elements:

  • append() - appends a single element to the list.
  • extend() - appends elements of an iterable to the list.
  • insert() - inserts a single item at a given position of the list.

All three methods modify the list in place and return None.

Python List append()

The append() method adds a single element to the end of the list .

The syntax of the append() method is as follows:

python
list.append(element) 

Where, element is the element to be added to the list.

Here is an example:

python
characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

characters.append('Nairobi')

print('Updated list:', characters)
output
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

The element parameter can be an object of any data type:

python
odd_numbers = [1, 3, 5, 7] 

even_numbers = [2, 4, 6]

odd_numbers.append(even_numbers)

print('Updated list:', odd_numbers)

The list even_numbers is added as a single element to the odd_numbers list.

output
Updated list: [1, 3, 5, 7, [2, 4, 6]]

Python List extend()

The extend() method all elements of an iterable to the end of the list.

The syntax of the extend() method is as follows:

python
list.extend(iterable) 

Where, iterable is the iterable to be added to the list.

python
characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

new_characters = ['Nairobi', 'Denver', 'Rio']

characters.extend(new_characters)

print('Updated list:', characters)
output
Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi', 'Denver', 'Rio']

The argument can be any type of iterable:

python
animals = ['dog', 'cat']

# tuple
mammals = ('tiger', 'elephant')

animals.extend(mammals)

print('Updated list:', animals)

# dictionary (see [dictionaries](https://linuxize.com/post/python-dictionaries/))
birds = {'owl': 1, 'parrot': 2}

animals.extend(birds)

print('Updated list:', animals)
output
Updated list: ['dog', 'cat', 'tiger', 'elephant']
Updated list: ['dog', 'cat', 'tiger', 'elephant', 'owl', 'parrot']

Python List insert()

The insert() method adds a single element to the list at the specified index.

The syntax of the insert() method is as follows:

python
list.insert(index, element) 

Where, index is the index of the element before which to insert, and the element is the element to be inserted in the list. In Python the list index starts with 0.

Here is an example:

python
fruits = ['raspberry', 'strawberry', 'blueberry'] 

fruits.insert(1, 'cranberry')

print('Updated list:', fruits)
output
Updated list: ['raspberry', 'cranberry', 'strawberry', 'blueberry']

The element parameter can be an object of any data type:

python
numbers = [10, 15, 20, 25] 

squares = [1, 4, 9]

numbers.insert(2, squares)

print('Updated list:', numbers)

The list squares is inserted as a single element to the numbers list.

output
Updated list: [10, 15, [1, 4, 9], 20, 25]

Conclusion

We have shown you how to add elements to a list in Python using the append(), extend(), and insert() methods. Another way to add elements to a list is to use the + operator to concatenate multiple lists.

If you have any questions or feedback, feel free to leave a comment.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page