Languages
[Edit]
EN

Python - list() constructor

0 points
Created by:
Chronal
697

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. 

Alternative titles

  1. Python - create list using constructor
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - list (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join