list() Function in Python:
The list() function is used to create a list object.
A list object is an ordered and changeable collection.
Syntax:
list(iterable)
Parameters
iterable: This is required. It could be a sequence, collection, or iterator object.
Return Value:
It returns a list.
- It returns an empty list if no parameters are passed.
- When iterable is passed as a parameter, it generates a list of iterable’s items.
Examples:
Example1:
Input:
Given tuple = (1, 2, 3, 4, 5) Given string = "hello" Given list = [10, 20, 30, 40]
Output:
The result after applying list() function on the given tuple = [1, 2, 3, 4, 5] The result after applying list() function on the given string = ['h', 'e', 'l', 'l', 'o'] The result after applying list() function on the given list = [10, 20, 30, 40]
Example2:
Input:
Given tuple = (25, 35, 45) Given string = "good" Given list = [1, 3, 5, 6]
Output:
The result after applying list() function on the given tuple = [25, 35, 45] The result after applying list() function on the given string = ['g', 'o', 'o', 'd'] The result after applying list() function on the given list = [1, 3, 5, 6]
list() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the tuple as static input and store it in a variable.
- Give the string as static input and store it in another variable.
- Give the list as static input and store it in another variable.
- Pass the given tuple as an argument to the list() function that converts a given tuple to a list and returns a list.
- Store it in another variable.
- Print the result list after applying the list() function on the given tuple.
- Similarly, do the same for the given string and list and print it.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as static input and store it in a variable. gvn_tupl = (1, 2, 3, 4, 5) # Give the string as static input and store it in another variable. gvn_str = "hello" # Give the list as static input and store it in another variable. gvn_list = [10, 20, 30, 40] # Pass the given tuple as an argument to the list() function that converts a given # tuple to a list and returns a list. # Store it in another variable. rslt1 = list(gvn_tupl) # Print the result list after applying list() function on the given tuple. print("The result after applying list() function on the given tuple = ", rslt1) # similarly do the same for the given string and list and print it. rslt2 = list(gvn_str) print("The result after applying list() function on the given string = ", rslt2) rslt3 = list(gvn_list) print("The result after applying list() function on the given list = ", rslt3)
Output:
The result after applying list() function on the given tuple = [1, 2, 3, 4, 5] The result after applying list() function on the given string = ['h', 'e', 'l', 'l', 'o'] The result after applying list() function on the given list = [10, 20, 30, 40]
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the tuple as user input using tuple (),map(),input(),and split() functions.
- Store it in a variable.
- Give the string as user input using the input() function and store it in another variable.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in another variable.
- Pass the given tuple as an argument to the list() function that converts a given tuple to a list and returns a list.
- Store it in another variable.
- Print the result list after applying the list() function on the given tuple.
- Similarly, do the same for the given string and list and print it.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as user input using tuple(),map(),input(),and split() functions. # Store it in a variable. gvn_tupl = tuple(map(int, input( 'Enter some random Tuple Elements separated by spaces = ').split())) # Give the string as user input using the input() function and # store it in another variable. gvn_str = input("Enter some random string = ") # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Pass the given tuple as an argument to the list() function that converts a given # tuple to list and returns a list. # Store it in another variable. rslt1 = list(gvn_tupl) # Print the result list after applying list() function on the given tuple. print("The result after applying list() function on the given tuple = ", rslt1) # similarly do the same for the given string and list and print it. rslt2 = list(gvn_str) print("The result after applying list() function on the given string = ", rslt2) rslt3 = list(gvn_lst) print("The result after applying list() function on the given list = ", rslt3)
Output:
Enter some random Tuple Elements separated by spaces = 3 4 5 1 Enter some random string = good Enter some random List Elements separated by spaces = 0 1 2 3 The result after applying list() function on the given tuple = [3, 4, 5, 1] The result after applying list() function on the given string = ['g', 'o', 'o', 'd'] The result after applying list() function on the given list = [0, 1, 2, 3]