Create list in python | Laptrinhcanban.com

HOME › >>

Create list in python

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]
['Hanoi' , 'Saigon', 'Cantho']

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' ]] 
for fruit in fruit basket:
print (fruit)

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
y = 15
numlist = [x, y]
print (numlist)

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
y = 15
numlist = [x, y]
print (numlist)

# Replace the value of x, y
x = 20
y = 25
print (numlist)

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
numlist = [0] * 10
print(numlist)

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'
numlist = ['null'] * 6
print(numlist)

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
print (numlist)

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 ()
print (nums)
# >> [1, 2, 3, 4, 5]

strs = list ( 'abcde' ) # creates list from string using list ()
print (strs)
# >> ['a', 'b', 'c', 'd', 'e']

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 )) 
print (nums1)

nums2 = list ( range (- 5 , 6 ))
print (nums2)

nums3 = list ( range ( 1 , 11 , 2 ))
print (nums3)

Result:

[0, 1, 2, 3, 4] 
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]

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. 
basket = [ 'Orange' , 'Apple' , [ 'Green Strawberry' , 'Red Strawberry' ]]
for fruit in basket:
print (fruit) # creates a list in python by indirectly specifying the element value via variable x = 10 y = 15 numlist = [x, y] print (numlist)

# create list in python by repeating the same elements with the number
numlist = [ 0 ] * 10
print (numlist)

# create list in python by repeating the same elements with the character
numlist = [ 'null' ] * 6
print (numlist)

# create list in python by repeating the same elements with the one in list other than
numlist = [ 10 , 20 , 30 ] * 3
print (numlist)

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/khoi-tao-list-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.