What is the method in Python for creating an empty list and assigning values to it?

In Python, there are two ways to create an empty list and assign values to it.

  1. These two paintings are identical in every way.
  2. “He’s finally decided to pursue his passion for music and start a band.”
my_list = []
my_list.append(1)
my_list.append(2)
print(my_list)  # 输出: [1, 2]
  1. Create a list with an initial value using list comprehension.
my_list = [1, 2, 3]
print(my_list)  # 输出: [1, 2, 3]

No matter which method, creating an empty list and assigning values only requires adding the values to the list object.

bannerAds