list() Function in Python

Python list() Function

The list() function in Python is a built-in function that creates a new list object. It can be used in several ways:

Conversion:

You can use the list() function to convert other iterable objects, such as tuples or strings, into a list. Here's an example:

my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)  # Output: [1, 2, 3]

my_string = "Hello"
my_list = list(my_string)
print(my_list)  # Output: ['H', 'e', 'l', 'l', 'o']

Empty List:

If you call list() without any arguments, it returns an empty list. For example:

empty_list = list()
print(empty_list)  # Output: []

Copying:

You can use list() to create a copy of an existing list. Modifying the new list won't affect the original list. Here's an example:

original_list = [1, 2, 3]
copied_list = list(original_list)
print(copied_list)  # Output: [1, 2, 3]

# Modifying the copied_list won't affect original_list
copied_list.append(4)
print(copied_list)      # Output: [1, 2, 3, 4]
print(original_list)    # Output: [1, 2, 3]

It's important to note that the list() function returns a new list object, which means it creates a separate copy of the original iterable or an empty list.