Posted at: https://laptrinhcanban.com/en
Let’s say you have a shopping list. Want to add an item to the list? Want to combine yesterday’s inventory with today?
Your requests can be handled by python in a flash, and we’ll learn how to add elements to list python, how to combine lists in python and how to repeat elements in list python with the following lesson.
Add elements to list python using the append method
We add an element to the python list using the method appendwith the following syntax:
list
.append
( value
)
This new element will be added to the end of the list, like in the following example:
mylist = ["A", "B", "C"] |
Remember, to add an element to the end of the list you use the method append() in python.
Add elements to list python with slice list
In the previous post we learned how to use the python list slicing function to change the value of multiple elements at the same time in python list .
In this article, we will learn more about how to use this function to add elements to the python list, with the following syntax:
list
[ len (list)
: len (list)
] = list, string etc..
he idea here is, we will replace the last part of the list with a new one. The last part of the list is defined by list[len(list): len(list)]
, so when we replace this part, a new string of elements (to the right of the mark =
) is added to the end of the original list.
Let’s see the following example for better understanding:
mylist = ["A", "B", "C"] |
You can see that the list specified to the right of the mark =
was added to the end of the original list.
Add elements of another list to the original list using the method extend()
When we want to add elements of another list to the original list, we use the method extend() like :
list
.extend( add_list
)
The elements inside add_list
(located to the right of the sign =) will be taken and added in to the original list. In addition to the list, you can also specify a tuple, or string, etc to add to the initial list.
See the following example:
mylist = ["A", "B", "C"] |
Distinguish different methods extendand methods appendas below:
- method append adds the argument as a new element to the original list
- method extend adds the elements of the argument to the original list.
For example:
#append method adds the new list as an element to the old list |
How to combine lists in python
To combine lists together in python, we use math +
to concatenate them and create a new list like so:
list A
+ list B
+ list C
+ …
For example:
list1 = ["A", "B"] |
Repeat elements in list python
We use math *
to iterate over the elements in the old list and create a new list like:
list
* số lần
For example
list = ["A", "B"] |
Summarize and practice
Above, Kiyoshi showed you how to add elements to a list and how to combine lists together 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.
#Ví dụ 1 |
URL Link
HOME › python for beginners>>list in python