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:

  1. iterable : an object can be string, tuple, set, dictionary or iterator iterative object

The value returned from the list

  1. If the parameter is not passed, list () will create an empty list
  2. 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

May be interested

  • Datetime in PythonPhoto of Datetime in Python
    in this article, we will work with you to learn how to handle dates and times in python with specific examples to make it easier to visualize and capture functions better. invites you to read the track.
  • Bookmark 5 best Python programming learning websitesPhoto of Bookmark 5 best Python programming learning websites
    if you are a developer or you are studying and want to stick with this industry, learn python to add a highlight in your journey.
  • 5 choose the best Python IDE for youPhoto of 5 choose the best Python IDE for you
    in order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
  • The vars () function in PythonPhoto of The vars () function in Python
    the vars () function in python returns the __dict__ attribute of the passed object if the object has the __dict__ attribute.
  • Tuple () function in PythonPhoto of Tuple () function in Python
    the built-in function tuple () in python is used to create a tuple.
  • Zip () function in PythonPhoto of Zip () function in Python
    the zip () function in python returns a zip object, a list iterator of tuples that combines elements from other iterators (made of iterable words).