In Python, the list() function is used to convert an iterable object (such as strings, tuples, dictionaries, sets, generators, etc.) into a list.
Function Syntax
list(iterable)
Parameters:
iterable: The iterable object to be converted into a list.
Returns a new list; if no parameters are passed, returns an empty list.
list() Function Examples
# Convert string to list
string = "Hello, World!"
my_list = list(string)
print(my_list) # Output: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
# Convert tuple to list
my_tuple = (1, 2, 3, 4, 5)
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3, 4, 5]
# Convert dictionary to list (returns list of keys)
my_dict = {"a": 1, "b": 2, "c": 3}
my_list = list(my_dict)
print(my_list) # Output: ['a', 'b', 'c']
Combine with range:
obj = range(5)
lst = list(obj)
print(lst) # [0, 1, 2, 3, 4]