Python list() function takes any iterable as a parameter and returns a list. In Python iterable is the object you can iterate over. Some examples of iterables are tuples, strings, and lists.
Python list() Function Syntax
Syntax: list(iterable)
Parameter:
- iterable: an object that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object.
Note: If we don’t pass any parameter then the list() function will return a list with zero elements (empty list).
list() Function in Python
We can create a Python list by using list() function. Below are the ways by which we can use list() function in Python:
- To create a list from a string
- To create a list from a tuple
- To create a list from set and dictionary
- Taking user input as a list
Example 1: Using list() to Create a List from a String
In this example, we are using list() function to create a Python list from a string.
Python
# initializing a string
string = "ABCDEF"
# using list() function to create a list
list1 = list(string)
# printing list1
print(list1)
Output['A', 'B', 'C', 'D', 'E', 'F']
Example 2: Using list() to Create a List from a Tuple
In this example, we are using list() function to create a Python list from a Tuple.
Python
# initializing a tuple
tuple1 = ('A', 'B', 'C', 'D', 'E')
# using list() function to create a list
list1 = list(tuple1)
# printing list1
print(list1)
Output['A', 'B', 'C', 'D', 'E']
Example 3: Using list() to Create a List from Set and Dictionary
In this example, we are using list() function to create a Python list from set and dictionary.
Python
# initializing a set
set1 = {'A', 'B', 'C', 'D', 'E'}
# initializing a dictionary
dictionary = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
# using list() to create a list
list1 = list(set1)
list2 = list(dictionary)
# printing
print(list1)
print(list2)
Output['A', 'C', 'B', 'E', 'D']
['A', 'C', 'B', 'E', 'D']
We can also use list() function while taking input from user to directly take input in form of a list.
Example 4: Taking User Input as a List
In this example, we are using list() function to take user input from the user and create a Python list from that input.
Python
# Taking input from user as list
list1 = list(input("Please Enter List Elements: "))
# printing
print(list1)
Output
Please Enter List Elements: 12345
['1', '2', '3', '4', '5']
Python list() Function – FAQs
How to get list functions?
To see the available functions and methods for lists in Python, you can use the dir() function with an instance of a list or the list class itself. For example:
print(dir(list))
This will display all the attributes, including methods available for list objects.
Is list() a built-in function?
Yes, list() is a built-in function in Python. It is used to create a new list object from any iterable, like tuples, strings, or other lists. If no iterable is given, it creates an empty list.
How to add lists in Python?
In Python, you can add two lists using the + operator, which concatenates them into a new list. For example:
list_one = [1, 2, 3]
list_two = [4, 5, 6]
combined_list = list_one + list_two
print(combined_list)
This will output: [1, 2, 3, 4, 5, 6].
4. What is a method in Python?
A method in Python is a function that is associated with an object. Methods perform specific actions on an object and can alter the object’s state or return a value. Methods are called on an object using dot notation. For example, the append() method adds an item to the end of a list:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
This will output: [1, 2, 3, 4].
5. What is get() in Python?
The get() method is used with dictionaries in Python. It retrieves the value for a given key in a dictionary. If the key does not exist, it returns None or a specified default value. This method is beneficial because it does not raise an error for missing keys like direct key access does. For example:
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict.get('name')) # Outputs: Alice
print(my_dict.get('address')) # Outputs: None
print(my_dict.get('address', 'No address provided')) # Outputs: No address provided