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.
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:
You can use a loop to iterate through a sequence of elements and append each piece to the list individually.
# Example listmy_list = [1, 2, 3]# Elements to appendelements_to_append = [4, 5, 6]# Append elements using a loopfor 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)
extend()
methodThe 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 listmy_list = [1, 2, 3]# Elements to appendelements_to_append = [4, 5, 6]# Append elements using extend() methodmy_list.extend(elements_to_append)print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Complexity
Time: O(k)
Space: O(1)
+
)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 listsmy_list = [1, 2, 3]elements_to_append = [4, 5, 6]# Concatenate two listsnew_list = my_list + elements_to_appendprint(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 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 listmy_list = [1, 2, 3]# Elements to appendelements_to_append = [4, 5, 6]# List comprehension to append elementsnew_list = [item for item in my_list] + elements_to_appendprint(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.
*
operatorThe *
operator can repeat a list and then combine it with another list.
# Example listmy_list = [1, 2, 3]# Elements to appendelements_to_append = [4, 5, 6]# Append elements using the * operatornew_list = my_list * 1 + elements_to_appendprint(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 allows you to replace a portion of the original list with the elements you want to append.
# Example listmy_list = [1, 2, 3]# Elements to appendelements_to_append = [4, 5, 6]# Append elements using slice assignmentmy_list[len(my_list):] = elements_to_appendprint(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.
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.
Free Resources