How to append multiple items to a list in Python

Share

In Python, a list is a versatile and fundamental data structure that stores elements in an ordered sequence. Lists are mutable, meaning their contents can be modified after creation. Square brackets denote them [ ] and can hold elements of different data types, including numbers, strings, and other lists.

Implementation

You can use various methods to append multiple items to a list in Python, such as a loop or the extend() technique and some others. Here are examples of possible approaches:

Using a loop

You can use a loop to iterate through a sequence of elements and append each piece to the list individually.

# Example list
my_list = [1, 2, 3]
# Elements to append
elements_to_append = [4, 5, 6]
# Append elements using a loop
for item in elements_to_append:
my_list.append(item)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(k)

  • Space: O(1)

Using the extend() method

The extend() method allows you to add multiple elements from an iterable (e.g., a list, tuple, or another sequence) to the end of the original list.

# Example list
my_list = [1, 2, 3]
# Elements to append
elements_to_append = [4, 5, 6]
# Append elements using extend() method
my_list.extend(elements_to_append)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(k)

  • Space: O(1)

List concatenation (+)

List concatenation involves two lists using the + operator to combine their elements and create a new list. This approach does not modify the original lists.

# Example lists
my_list = [1, 2, 3]
elements_to_append = [4, 5, 6]
# Concatenate two lists
new_list = my_list + elements_to_append
print(new_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(n+k)

  • Space: O(n+k)

Here "n" is the number of elements in the original list, and "k" is the number of elements to be appended.

List comprehension

List comprehension is a concise way to create a new list by applying an expression to each element of an iterable (such as another list) and optionally using a condition.

# Example list
my_list = [1, 2, 3]
# Elements to append
elements_to_append = [4, 5, 6]
# List comprehension to append elements
new_list = [item for item in my_list] + elements_to_append
print(new_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(n+k)

  • Space: O(n+k)

Here "n" is the number of elements in the original list, and "k" is the number of elements to be appended.

Using the * operator

The * operator can repeat a list and then combine it with another list.

# Example list
my_list = [1, 2, 3]
# Elements to append
elements_to_append = [4, 5, 6]
# Append elements using the * operator
new_list = my_list * 1 + elements_to_append
print(new_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(n+k)

  • Space: O(n+k)

Here "n" is the number of elements in the original list, and "k" is the number of elements to be appended.

Slice assignment

Slice assignment allows you to replace a portion of the original list with the elements you want to append.

# Example list
my_list = [1, 2, 3]
# Elements to append
elements_to_append = [4, 5, 6]
# Append elements using slice assignment
my_list[len(my_list):] = elements_to_append
print(my_list) # Output: [1, 2, 3, 4, 5, 6]

Complexity

  • Time: O(n+k)

  • Space: O(1)

Here "n" is the number of elements in the original list, and "k" is the number of elements to be appended.

Conclusion

Remember that some methods create new lists, while others modify the original list. The choice of method depends on whether you want to keep the original list unchanged or directly change it with the appended elements. Additionally, the efficiency and readability of each method can vary, so it’s essential to consider the context in which you’re using them.

Among the methods mentioned, the extend() method is the most efficient for appending multiple elements to a list in Python. Its efficiency is because it modifies the original list in place and performs the operation in a single step, directly adding the elements from the iterable to the end of the list.

Copyright ©2024 Educative, Inc. All rights reserved