In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what are the four methods added to the list in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the four methods added to the list in python" can help you solve the problem.
Add list 1) + add
2) append append
Only one element can be added to the list at a time, suitable for use in loops
3) extend stretching
You can add multiple elements to the list at a time
4) insert insertion
Both append and extend are added at the end, and insert can be inserted in the specified location.
The command functions service.insert (n _ journal'') to insert the contents of''into the nth and then service.insert (1 ~ th _ samba') inserts the samba after the first
Exercise:
Insert after the third one
After inserting into the second one
Insert two
The results show that firewalld is after samba. Because after the samba is inserted into the first, it becomes the second; at this point, the firewalld is inserted into the second, just after the samba.
Add an element at the specified location in the list
Python also provides the insert () method, which can insert an element at any specified location in the list. Its basic syntax is:
Source_list.insert (index,obj) in which
Source_list: for the list to be modified
Index: index the location to be inserted
Obj: the element to be inserted
Note: in Python, the position index of the start element of the list is 0. For example, to add the guest Hu qi after the Zhang san in the guests list, the corresponding statement is:
# create and initialize guests list guests= ['Zhang san','Li si','Wang wu','Zhao liu'] # add a guest guests.insert named Hu qi to the end of the guests list Zhang san (1) guests Hu qi' # output a new guests list print (guests)
The output is as follows:
['Zhang san','Hu qi','Li si','Wang wu','Zhao liu']
This is the end of the content about "what are the four ways to add lists in python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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: 251
*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.