You can use + and * operation to extend a list.
List Add (+
) Operation
Add (+
) operator is used to combine (concatenates) several lists into a single list. It is similar to list.extend()
method.
Example:
list1 = [1, 2] list2 = [3, 4] list3 = [5, 6] combined_list = list1 + list2 + list3 print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
List Multiply (*
) Operation
Multiply (*
) operator will create a new list by repeating the elements of the original list a specified number of times.
Example:
original_list = [1, 2, 3] replicated_list = original_list * 3 print(replicated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Caution
This replication is a shallow copy. Especially, if the elements in list are mutable objects (like lists or dictionaries).
Exampe:
nested_list = [[1, 2], [3, 4]] replicated_nested_list = nested_list * 2 replicated_nested_list[0][0] = 5 print(replicated_nested_list) # Output: [[5, 2], [3, 4], [5, 2], [3, 4]]