The python list creates a list in python.
Python list Function Syntax
It has the following syntax:
Example
list(iterable)
Parameters
- iterable (optional): This refers to an object that may take the form of a sequence (such as a string, tuple, etc.) or a collection (like a set, dictionary, etc.), or it can also be an iterator object.
Return
It returns a list.
Different Examples for Python list Function
In this section, we will explore a variety of examples demonstrating the functionality of the Python list function.
Python list Function Example 1
The following example demonstrates how to generate a list from a sequence, which can include a string, a tuple, and another list.
Example
# empty list
print(list())
# string
String = 'abcde'
print(list(String))
# tuple
Tuple = (1,2,3,4,5)
print(list(Tuple))
# list
List = [1,2,3,4,5]
print(list(List))
Output:
Output
[]
['a', 'b', 'c', 'd', 'e']
[1,2,3,4,5]
[1,2,3,4,5]
Explanation:
The preceding example generates a list derived from a sequence that includes a string, a tuple, and another list.
Python list Function Example 2
The following example demonstrates how to generate a list from a collection, specifically utilizing a set and a dictionary.
Example
# set
Set = {'a', 'b', 'c', 'd', 'e'}
print(list(Set))
# dictionary
Dictionary = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'e':5}
print(list(Dictionary))
Output:
Output
['e', 'c', 'd', 'b', 'a']
['c', 'e', 'd', 'b', 'a']