What is the purpose of the list function in Python?
The list function in Python is used to convert an iterable object (such as a string, tuple, set, etc.) into a list. It allows for easy manipulation and processing of lists by converting other data types.
For example, the list function can be used to convert a string into a list of characters.
s = "Hello"
lst = list(s)
print(lst) # 输出: ['H', 'e', 'l', 'l', 'o']
Similarly, a tuple can be converted into a list.
t = (1, 2, 3, 4)
lst = list(t)
print(lst) # 输出: [1, 2, 3, 4]
The list function can also be used to create an empty list:
empty_lst = list()
print(empty_lst) # 输出: []
Besides using the list function, you can also create a list using square brackets [], and both methods will have the same effect.