Jun 16, 2023 4 min read

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

Add Elements to a List in Python with our tutorial. A Python list is an ordered collection of items enclosed within square brackets.

Add Elements to a List in Python (append, extend and insert)
Table of Contents

Before we begin talking about how to add elements to a list in Python, let's briefly understand – What is a list in Python?

A list in Python is an ordered collection of items enclosed within square brackets. It allows storing multiple values, such as numbers, strings, or even other lists, in a single variable. Lists are versatile and flexible, offering various operations like adding, removing, or modifying elements. With its indexing and slicing capabilities, accessing specific elements becomes easy.

Lists are commonly used in programming for tasks like storing data, iterating over items, and implementing algorithms efficiently. Python's list data structure provides a powerful tool for managing and manipulating data effectively in your programs.

In this tutorial, we will discuss how to add elements to a list in Python. We will also address a few FAQs on how to add elements to a list in Python.

Advantages of Lists in Python

  1. Versatility: Python lists can hold diverse data types, allowing flexibility in storing and manipulating various elements.
  2. Dynamic Size: Lists can grow or shrink dynamically, making it convenient to add or remove elements as needed.
  3. Indexing and Slicing: Lists support indexing and slicing operations, enabling easy access to specific elements or sublists.
  4. Iteration: Lists are iterable, making it simple to loop through elements using loops or comprehensions.
  5. Common Operations: Lists offer common operations like sorting, concatenation, and duplication, simplifying data management tasks in Python programming.

Python List append()

One element is added to the end of the list using the append() method.

The append() method has the following syntax:

list.append(element) 

Where element is the element that will be included in the list.

Below is an example:

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

characters.append('Nairobi')

print('Updated list:', characters)
Output

Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

Any object with a data type can be the element parameter:

odd_numbers = [1, 3, 5, 7] 

even_numbers = [2, 4, 6]

odd_numbers.append(even_numbers)

print('Updated list:', odd_numbers)

A single entry from the list even_numbers is added to the list odd_numbers.

Output

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

Python List extend()

An iterable's items are extended to the end of the list using the extend() method.

The extend() method has the following syntax:

list.extend(iterable) 

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

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']

Any iterable type can be used for the argument:

animals = ['dog', 'cat']

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

animals.extend(mammals)

print('Updated list:', animals)

# dictionary
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() function adds a single element to the list at the given index.

The insert() method is written in the following syntax:

list.insert(index, element) 

Where index denotes the index of the element before which to insert, and the element denotes the element that is to be added to the list. The list index in Python starts at 0.

Below is an example:

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:

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]

FAQs to Add Elements to a List in Python

Can I add multiple elements to a list at once?

Yes, you can use the extend() method or the + operator to add multiple elements or another list to an existing list.

How can I add elements to a list while preserving the original list?

You can create a new list by concatenating the original list with the elements you want to add using the extend() method or the + operator.

Is it possible to add elements at a specific position in a list?

Yes, you can use the insert() method to add elements at a specific index in the list, shifting the existing elements to accommodate the new one.

How can I add elements to the beginning of a list?

You can use the insert() method with an index of 0 to add elements at the beginning of the list, pushing the existing elements forward.

What if I want to add elements to a list in a sorted order?

You can use the bisect.insort() function from the bisect module to add elements to a sorted list while maintaining its sorted order.

Can I add elements to a list using list comprehension?

Yes, you can use list comprehension to create a new list with additional elements based on certain conditions or transformations applied to existing elements.

Is it possible to add elements to a list using the "+=" operator?

No, the += operator is not supported for directly adding elements to a list. It is used for concatenation, not element addition.

Conclusion

The append(), extend(), and insert() methods have been used to demonstrate how to add elements to a list in Python. The + operator can be used to concatenate several lists, which is another method of adding elements to a list.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.