EN
Python - list() constructor
0
points
In this article, we would like to show you how to use list() constructor in Python.
Quick solution:
my_list = list(('A', 'B', 'C'))
print(my_list) # ['A', 'B', 'C']
Practical examples
Example 1
In this example, we use list()
constructor when creating a new list.
my_list = list(('A', 'B', 'C'))
print(my_list) # ['A', 'B', 'C']
Output:
['A', 'B', 'C']
Example 2
Using list()
constructor, we can also create list from string, tuple, dictionary or set like this:
my_string = 'string'
result1 = list(my_string)
my_tuple = ('t', 'u', 'p', 'l', 'e')
result2 = list(my_tuple)
my_dictionary = {'d': 1, 'i': 2, 'c': 3, 't':4}
result3 = list(my_dictionary)
my_set = {'s', 'e', 't'}
result4 = list(my_set)
print(result1) # ['s', 't', 'r', 'i', 'n', 'g']
print(result2) # ['t', 'u', 'p', 'l', 'e']
print(result3) # ['d', 'i', 'c', 't']
print(result4) # ['s', 'e', 't']
Output:
['s', 't', 'r', 'i', 'n', 'g']
['t', 'u', 'p', 'l', 'e']
['d', 'i', 'c', 't']
['s', 'e', 't']
Note:
The order of the elements in set will be random.