How can multiple elements be added to an empty list in Python?
You can use the extend() function of a list to add multiple elements to an empty list. The extend() function can take an iterable object (such as a list, tuple, string, etc.) and add its elements to the list one by one.
For instance, the following code will add multiple elements to an empty list:
my_list = [] # 创建一个空列表
new_elements = [1, 2, 3, 4] # 新增的元素
my_list.extend(new_elements) # 将new_elements中的元素添加到my_list中
print(my_list) # 输出:[1, 2, 3, 4]
Additionally, you can use the “+” operator on lists to add multiple elements to an empty list, as shown below:
my_list = [] # 创建一个空列表
new_elements = [1, 2, 3, 4] # 新增的元素
my_list += new_elements # 将new_elements中的元素添加到my_list中
print(my_list) # 输出:[1, 2, 3, 4]
Both the extend() function and the “+” operator can be used to add multiple elements to an empty list.