List () function in Python
List () creates a list in Python. So what is the syntax of list () function, what parameters does it have and how to use it? Invites you to read the track.
The syntax of list () function in Python
list([iterable])
Parameter of list function ()
The list () list creation function in Python has a unique parameter:
- iterable : an object can be string, tuple, set, dictionary or iterator iterative object
The value returned from the list
- If the parameter is not passed, list () will create an empty list
- If iterable is passed as a parameter, it creates a list of iterable elements
Example 1: Create list from string, tuple, list
# danh sách trống
print(list())
# chuỗi nguyên âm
nguyenamString = 'aeiou'
print(list(nguyenamString))
# tuple nguyên âm
# viết bởi TipsMake.com
nguyenamTuple = ('a', 'e', 'i', 'o', 'u')
print(list(nguyenamTuple))
# danh sách nguyên âm
nguyenamList = ['a', 'e', 'i', 'o', 'u']
print(list(nguyenamList))
Run the program, the result is:
[]
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
Example 2: Create list of words set, dictionary
# set nguyên âm
nguyenamSet = {'a', 'e', 'i', 'o', 'u'}
print(list(nguyenamSet))
# dictionary nguyên âm
nguyenamDictionary = {'a': 1, 'e': 2, 'i': 3, 'o':4, 'u':5}
print(list(nguyenamDictionary))
Running the program results in:
['e', 'o', 'a', 'i', 'u']
['e', 'o', 'u', 'a', 'i']
Example 3: Create list from iterator object
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
powTwo = PowTwo(5)
powTwoIter = iter(powTwo)
print(list(powTwoIter))
Running the program results in:
[1, 2, 4, 8, 16]
See also: Built-in Python functions
5 ★ | 1 Vote
You should read it
May be interested
- The map () function in Pythoncontinuing with the topic of built-in functions in python, the article will introduce you to the map () function with syntax, usage as well as specific examples. invites you to read the track.
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.