Sign In
Sign In

How to Add Elements to a List in Python

How to Add Elements to a List in Python
Hostman Team
Technical writer
Python
18.10.2024
Reading time: 6 min

In many programming languages, arrays are used as the primary structure for storing data. An array is an ordered set of elements of the same data type. However, not all programming languages have arrays. Specifically, in Python, lists are used instead of arrays.

A list is a data structure for storing objects of various data types. In a sense, it can be considered a "dynamic" array: Python lists impose almost no limits on the number of items (the maximum size depends on the sys.maxsize parameter and the operating system's bitness) or the types of those items.

In this article, we’ll explain how to add items to a Python list.

Re-declaration

The simplest and most "crude" way to add an element to a list in Python is to re-declare the object:

example_list = [10, 11, 12]
print(example_list)
example_list = [10, 11, 12, 13]
print(example_list)

Output:

[10, 11, 12]
[10, 11, 12, 13]

This method isn't really worth focusing on and is only useful if you’ve forgotten all other alternatives.

The append() Method

The append() method will help if you need to add a new element to a Python list. The new object is added at the end. Here is the syntax for this method:

AppendList.append(object)

Where:

  • AppendList is the list to which the element is being added;
  • object is the new object.

Here's an example of adding an element to the end of a Python list using append():

AppendList = [11, 12, 13]
print(AppendList)

AppendList.append(14)
print(AppendList)

AppendList.append('another type')
print(AppendList)

Output:

[11, 12, 13]
[11, 12, 13, 14]
[11, 12, 13, 14, 'another type']

In Python, you can also add an element to an empty list using append():

AppendList = []
AppendList.append('hostman')
print(AppendList)

Output:

['hostman']

The new object can even be another list:

AppendList = [11, 12, 13]
AddList = [14, 15]
AppendList.append(AddList)
print(AppendList)

Output:

[11, 12, 13, [14, 15]]

Here, we added the list AddList as an object into the AppendList. If you need to add the individual elements of one list to another, you should use the extend() method instead.

The extend() Method

The extend() method allows you to merge two lists. Here is the syntax for the method:

ExtendList.extend(iterable_object)

Where:

  • ExtendList is the list to which the elements will be added.

  • iterable_object is the object whose elements will be added to ExtendList.

Let's move to a practical example. Here's how to add multiple elements to a Python list using extend():

ExtendList = [11, 12, 13]
AddList = [14, 15]
ExtendList.extend(AddList)
print(ExtendList)

Output:

[11, 12, 13, 14, 15]

The extend() method also works with any iterable objects.

What is an iterable object?

An iterable object is an object that can return its elements one by one. Lists, tuples, dictionaries, and strings are all iterable objects.

Let’s see how extend() works with these types of objects:

ExtendList = [11, 12, 13]
AddDictionary = {'first': 1, 'second': 2}
AddString = 'host.man'
AddTuple = (14, 15, 16, 17)

ExtendList.extend(AddDictionary)
print('Extend with dictionary:', ExtendList)

ExtendList.extend(AddString)
print('Extend with string:', ExtendList)

ExtendList.extend(AddTuple)
print('Extend with tuple:', ExtendList)

Output:

Extend with dictionary: [11, 12, 13, 'first', 'second']
Extend with string: [11, 12, 13, 'first', 'second', 'h', 'o', 's', 't', '.', 'm', 'a', 'n']
Extend with tuple: [11, 12, 13, 'first', 'second', 'h', 'o', 's', 't', '.', 'm', 'a', 'n', 14, 15, 16, 17]

Instead of using extend(), you can also use the + operator to achieve the same result:

ExtendList = [11, 12, 13]
AddList = [14, 15, 16]
ExtendList += AddList
print(ExtendList)

ExtendList = ExtendList + AddList
print(ExtendList)

Output:

[11, 12, 13, 14, 15, 16]
[11, 12, 13, 14, 15, 16, 14, 15, 16]

The insert() Method

To add an element to a Python list at a specific index, you can use the insert() method. This places the element in a user-specified position. Here's the syntax:

InsertList.insert(pos, element)

Where:

  • InsertList is the list to which the element will be added.
  • pos is the position of the new element.
  • element is the object to be added.

Here's how to add an element to the beginning of a list in Python:

InsertList = [11, 12, 13]
InsertList.insert(0, 10)
print(InsertList)

Output:

[10, 11, 12, 13]

Other examples:

InsertList = [11, 12, 13]
InsertList.insert(1, 'b')
print(InsertList)

InsertList.insert(4, 'c')
print(InsertList)

Output:

[11, 'b', 12, 13]
[11, 'b', 12, 13, 'c']

To add an element to the middle of a list:

InsertList = [11, 12, 13]

InsertList.insert(len(InsertList)//2, 'a')
print(InsertList)

InsertList.insert(len(InsertList)//2, 'middle')
print(InsertList)

Output:

[11, 'a', 12, 13]
[11, 'a', 'middle', 12, 13]

If the index is out of bounds, no error will occur. Python will place the element at the far left or far right:

InsertList = [11, 12, 13]

InsertList.insert(-20, 'a')
print(InsertList)

InsertList.insert(20, 'b')
print(InsertList)

Output:

['a', 11, 12, 13]
['a', 11, 12, 13, 'b']

Concatenation

Concatenation is the operation of joining several linear structures, like strings. For instance, concatenating the strings "host" and "man" results in "hostman."

Concatenation Using the Asterisk *:

ConcatenatedList = [11, 12, 13]

new_list = [ConcatenatedList, 14]
print(new_list)

new_list = [*ConcatenatedList, 14]
print(new_list)

Output:

[[11, 12, 13], 14]
[11, 12, 13, 14]

As you can see, in the first case, the object itself is added, while in the second case, its content is added.

Concatenation Using a Nested Loop:

FirstList = [11, 12, 13, 14, 15]
SecondList = [16, 17, 18]
result = [j for i in [FirstList, SecondList] for j in i]
print(result)

Output:

[11, 12, 13, 14, 15, 16, 17, 18]

Concatenation Using itertools.chain():

import itertools
FirstList = [11, 12, 13, 14, 15]
SecondList = [16, 17, 18]
result = list(itertools.chain(FirstList, SecondList))
print(result)

Output:

[11, 12, 13, 14, 15, 16, 17, 18]

Adding Elements Using Slices

Slices are used to retrieve a subset of a list, but they can also be used to add or replace elements in a list. The syntax for slices is:

SliceList[START_POS:STOP_POS:STEP]

Where:

  • SliceList is the list you're working with.
  • START_POS is the index of the first element to include.
  • STOP_POS is the index that marks the end of the slice (not included in the result).
  • STEP is the interval between elements.

Here's an example:

SliceList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
new_list = SliceList[0:10:2]
print(new_list)

Output:

[0, 2, 4, 6, 8]

You can also use slices to add elements to a list by appending or replacing part of it. Here’s the syntax for adding:

SliceList[START:] = iterable

Where:

  • SliceList is the list to modify.
  • START is the position where new elements will be inserted.
  • iterable is the iterable object whose elements will be added.

Example with a String:

slice_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
slice_string = 'hostman'
slice_list[len(slice_list):] = slice_string
print(slice_list)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'h', 'o', 's', 't', 'm', 'a', 'n']

Example with Another List:

slice_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
slice_list2 = [10, 11, 12]
slice_list[len(slice_list):] = slice_list2
print(slice_list)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Replacing Part of a List Using Slices:

odd_list = [1, 1, 3, 3, 5, 5, 7, 7, 9, 9]
even_list = [0, 2, 4, 6, 8]
odd_list[0:10:2] = even_list
print(odd_list)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Conclusion

This material covered various methods for adding elements to a Python list.  Most of the time, the functionality of common methods like extend(), insert(), and append() will be sufficient. However, knowing about other techniques such as concatenation and slices is also useful.

Python
18.10.2024
Reading time: 6 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us