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