In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Python how to add elements at the end of the list". In daily operation, I believe many people have doubts about how to add elements at the end of the list in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how Python adds elements at the end of the list". Next, please follow the editor to study!
Add an element at the end of the list
In Python, you can use the append () method to append an element to the end of a list. The basic syntax is as follows:
Source_list.append (obj)
Among them
Source_list: for the list to be modified
Obj: the element to be inserted
For example, to add a guest Hu qi to the end of the guests list, the corresponding statement is:
# initialize the guests list guests= ['Zhang san','Li si','Wang wu','Zhao liu'] # append a guest guests.append named Hu qi to the end of the guests list (' Hu qi') # output the new guests list print (guests)
The output is as follows:
['Zhang san','Li si','Wang wu','Zhao liu','Hu qi']
The difference between adding elements append (), extend (), insert (), + list to the list
Recall that when I first learned python, when I added elements to the list list, I was always confused about the methods of adding class tables, append () and extend (), insert (), and so on.
The following is to understand their differences through definitions and code demos.
1. Append () appends a single element to the tail of List
Only one parameter is accepted, the parameter can be any data type, and the appended element maintains the original structure type in the List.
#-*-coding:utf-8-*-# declare two lists: list1 and list2list1= ['1','1','2','2','4'] # append () list1.append (list2) # print the current list1print (list1) # list.append (object) is to add an object object to the list.
2. Extend () adds each element in one list to another list
Only one parameter is accepted.
# declare two lists list1list1= ['hobbies list1list1=] # append () list1.extend (list2) # print the current list1print (list1) # extend () list.extend (sequence) to add the contents of a sequence seq to the list.
3. Insert () inserts an element into the list
But it has two parameters (such as insert (1, "g"). The first parameter is the index point, that is, the position of the insertion, and the second parameter is the inserted element.
# declare two lists of list1list1= ['Achievement insert] # insert (), and insert an element' X'list1.insert (1memento X') # to print the current list1print (list1) before the second element of the list1print.
4. + plus sign to add two list
It will return to a new list object and notice the difference from the first three. The first three methods (append, extend, insert) add elements to the list. They do not return a value, but modify the original data object directly. Note: to add two list together, you need to create a new list object, which consumes extra memory, especially when the list is large, try not to use "+" to add list, but should use List's append () method as much as possible.
At this point, the study on "how Python adds elements at the end of the list" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.