Add elements to list python and how to combine lists in python | Laptrinhcanban.com

HOME › >>

Add elements to list python and how to combine lists in python

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"]
mylist.append("D")

print(mylist)
#--> ["A", "B", "C", "D"]

mylist.append("K")
print(mylist)
#--> ["A", "B", "C", "D","K"]

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"]
print(mylist[len(mylist):len(mylist)])
#-->[]
# At the end of the list, there are no elements. result returns an empty list.

mylist[len(mylist):len(mylist)] = ["D", "E"]

print(mylist)
#--> ["A", "B", "C", "D", "E"]

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"]
mylist.extend(["D", "E"])

print(mylist)
#--> ["A", "B", "C", "D", "E"]

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
mylist = ["A", "B", "C"]
mylist.append(["D", "E"])

print(mylist)
#--> ["A", "B", "C", ["D", "E"]]

#the extend method adds each element of the new list to the old list
mylist = ["A", "B", "C"]
mylist.extend(["D", "E"])

print(mylist)
##--> ["A", "B", "C", "D", "E"]

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"]
list2 = ["C", "D"]
list3 = ["E", "F"]

list4 = list1 + list2 + list3
print(list4)

#-->['A', 'B', 'C', 'D', 'E', 'F']

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"]
newlist = list * 3
print(newlist)

#--> ['A', 'B', 'A', 'B', '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
mylist = ["A", "B", "C"]
print(mylist[len(mylist):len(mylist)])

mylist[len(mylist):len(mylist)] = ["D", "E"]

print(mylist)


#Example 2
mylist = ["A", "B", "C"]
mylist.extend(["D", "E"])

print(mylist)
#--> ["A", "B", "C", "D", "E"]

#append method
mylist = ["A", "B", "C"]
mylist.append(["D", "E"])

print(mylist)
#--> ["A", "B", "C", ["D", "E"]]

#extend methods
mylist = ["A", "B", "C"]
mylist.extend(["D", "E"])

print(mylist)
##--> ["A", "B", "C", "D", "E"]

#Example 3
list1 = ["A", "B"]
list2 = ["C", "D"]
list3 = ["E", "F"]

list4 = list1 + list2 + list3
print(list4)

#Example 4
list = ["A", "B"]
newlist = list * 3
print(newlist)

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/them-phan-tu-vao-list-ket-hop-cac-list-voi-nhau-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.