Table of Contents
In this tutorial, we will learn about the Python list() constructor with the help of examples.
The list() constructor returns a list in Python.
Example
text = 'Python' # convert string to list text_list = list(text) print(text_list) # check type of text_list print(type(text_list)) # Output: ['P', 'y', 't', 'h', 'o', 'n'] # <class 'list'>
1. list() Syntax
The syntax of list() is:
list([iterable])
2. list() Parameters
The list() constructor takes a single argument:
- iterable (optional) – an object that could be a sequence (string, tuples) or collection (set, dictionary) or any iterator object
3. list() Return Value
The list() constructor returns a list.
- If no parameters are passed, it returns an empty list
- If iterable is passed as a parameter, it creates a list consisting of iterable’s items.
4. Example 1: Create lists from string, tuple, and list
# empty list
print(list())
# vowel string
vowel_string = 'aeiou'
print(list(vowel_string))
# vowel tuple
vowel_tuple = ('a', 'e', 'i', 'o', 'u')
print(list(vowel_tuple))
# vowel list
vowel_list = ['a', 'e', 'i', 'o', 'u']
print(list(vowel_list))
Output
[] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u']
5. Example 2: Create lists from set and dictionary
# vowel set
vowel_set = {'a', 'e', 'i', 'o', 'u'}
print(list(vowel_set))
# vowel dictionary
vowel_dictionary = {'a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5}
print(list(vowel_dictionary))
Output
['a', 'o', 'u', 'e', 'i'] ['o', 'e', 'a', 'u', 'i']
Note: In the case of dictionaries, the keys of the dictionary will be the items of the list. Also, the order of the elements will be random.
6. Example 3: Create a list from an iterator object
# objects of this class are iterators
class PowTwo:
def __init__(self, max):
self.max = max
def __iter__(self):
self.num = 0
return self
def __next__(self):
if(self.num >= self.max):
raise StopIteration
result = 2 ** self.num
self.num += 1
return result
pow_two = PowTwo(5)
pow_two_iter = iter(pow_two)
print(list(pow_two_iter))
Output
[1, 2, 4, 8, 16]
Recommended Reading: Python List
Related posts:
Node.js vs Python for Backend Development
Python Dictionary update()
Python String rpartition()
Python str()
Python String rjust()
Python Directory and Files Management
Python int()
Python Program to Get Line Count of a File
Python Dictionary clear()
Python Program to Find the Factors of a Number
Python Program to Find the Size (Resolution) of a Image
Python Set symmetric_difference()
Python Program to Count the Occurrence of an Item in a List
Python List extend()
Python Program to Find Numbers Divisible by Another Number
Python Program to Print the Fibonacci sequence
Python enumerate()
Python Matrices and NumPy Arrays
Python Numbers, Type Conversion and Mathematics
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python locals()
Python hash()
Python Program to Remove Punctuations From a String
Python List reverse()
Python Set symmetric_difference_update()
Python frozenset()
Python Program to Measure the Elapsed Time in Python
Python Program to Find LCM
Python Set issuperset()
Python Errors and Built-in Exceptions
Python String format_map()