Posted at: https://laptrinhcanban.com/en
Instructions on how to create list in python. You will learn how to create list in python by specifying a value , create list in python using variable , create list in python using multiplication operator, create list in python using list () function, create list in python using range after this lesson.
Create list in python by specifying a value
To create a list in python, we specify the value of the elements inside the ampersand pair []
and separated by signs ,
with the following syntax:
[Element 1
, Element 2
, …]
Elements can be any form of data Python can handle, such as numbers and strings.
For example:
[10, 20, 30, 40, 50] |
You can also use different data types for each element in the same list.
['Kiyoshi', 30 , True] |
Alternatively, you can also use a list as an element in another list.
['Cam' , 'Táo' , ['Dâu xanh' , 'Dâu đỏ']] |
You can also create an empty list in python with the following writing:
[] |
You can also assign a list to a variable for later use.
basket = [ 'Orange' , 'Apple' , [ 'Green Strawberry' , 'Red Strawberry' ]] |
Result
Oranges Apple ['Green Strawberry', 'Red Strawberry']
Create list in python using variables
Using this approach, instead of directly specifying the value of an element in the list, we will assign that value to a variable and use the variable’s value to initialize the list.
For example:
x = 10 |
Result:
[10, 15]
It should be noted here that we only use the value of the variable to initialize the element value when creating the list, rather than creating a reference relationship between the variable and the elements in the list.
Therefore, after creating the list, if we change the value of the variable, then the value of the element in the list will not be changed.
For example:
x = 10 |
Kết quả:
[10, 15] [10, 15]
Create list python using multiplication operator
When in a list containing the same elements, we can iterate over a list and create a new list using the multiply operator with the following syntax:
List
= [
element]
*
number
Example with numbers:
# khởi tạo một list gồm 10 số 0 |
Result:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
String example:
# khởi tạo một list gồm 6 chuỗi 'null' |
Result:
['null', 'null', 'null', 'null', 'null', 'null']
This is very convenient when you want to create a list with some specific elements that need to be initialized.
Alternatively, you can use this iteration to iterate over a list in another list, as shown in the following example:
numlist = [10, 20, 30] * 3 |
Result:
[10, 20, 30, 10, 20, 30, 10, 20, 30]
Create list in python using the list () function
The list () function is commonly used to create list in python with the following syntax:
newlist= list( iterable)
In which iterable are objects with many elements such as string, tuple, dictionary and so on.
For example:
nums = list (( 1 , 2 , 3 , 4 , 5 )) # creates list from tuple using the function list () |
Detailed instructions on this can be found at the article Create list by using iterable in python .
Create list in python using range
By combining the list () function and the range () function, you can create a list whose elements are a sequence of consecutive numbers, with the following syntax:
newlist= list( range(start, end, step))
For example:
nums1 = list ( range ( 5 )) |
Result:
[0, 1, 2, 3, 4] |
Detailed instructions on this can be found at the article Create list by using iterable in python.
Summarize
Above, Kiyoshi showed how to create list in python . To better understand the lesson content, you should practice with the following examples.
And let’s learn more about Python in the next lessons.
# creates list in python by directly specifying elements values. |
URL Link
HOME › python for beginners>>list in python