List additions and deletions in Python and other related methods

Source: Internet
Author: User

1. There are three ways to add
1 #"", "" Three Ways2 3 #Append ()4Li = ['python','Java','C + +']5Li.append (['these are all programming languages .'])6 Print(LI)#output results: [' python ', ' Java ', ' C + + ', [' These are programming languages ']]7            #also note that append () is not a return value, such as: Print (Li.append ()), and the return result is None8 9 #Insert () is added by index, in the previous bit of the indexTenLi = ['python','Java','C + +'] OneLi.insert (2,'I love Python .') A Print(LI)#output Result: [' python ', ' Java ', ' I love python ', ' C + + '] -  - #extend () iteration to add theLi = ['python','Java','C + +'] -Li.extend ('AAAAA') - Print(LI)#output results: [' python ', ' Java ', ' C + + ', ' a ', ' a ', ' a ', ' a ', ' a ']
2. Deletion of four methods
1 # "" ""2 3 #pop ()4Li = ['python','Java','C + +']5Li.pop (1)6 Print(LI)#output Result: [' Python ', ' C + + '], if Li.pop (), the last element is deleted by default7 8 #Remove ()9Li = ['python','Java','C + +']TenLi.remove ('Java') One Print(LI)#output Result: [' Python ', ' C + + '] A  - #del: Delete According to the location of the slice -  theLi = ['python','Java','C + +'] - delLi[1:2] - Print(LI)#output Result: [' Python ', ' C + + '] -  + #Clear (): Empty list -  +Li = ['python','Java','C + +'] A li.clear () at Print(LI)#output Result: []

3. Change according to index or slice
1 #Change : According to the index or slice to change2 3Li = ['python','Java','C + +']4LI[2] ='C'5 Print(LI)#output Result: [' python ', ' Java ', ' C ']6Li = ['python','Java','C + +']7Li[0:1] ='I must insist on learning Python'8 Print(LI)#output: [' I ', ' one ', ' Set ', ' firm ', ' Hold ', ' learn ', ' XI ', ' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ' Java ', ' C + + ']
4. Check the cycle or slice to check 5. Public Method 5.1 Len () method
1 # Len () 2 li = ['python''java''C + + ' ]3print(len (LI))
5.2 Count () method
1 # Coount () 2 li = ['python''java''C + + ' ]3print(Li.count ('python'))
5.3 Index () method
1 # index () 2 li = ['python''java''C + + ' ]3print(Li.index ('python'))
6. Sorting
1 #forward Sort2Li = [1,3,6,4,8,2, 0]3 Li.sort ()4 Print(LI)#output results: [0, 1, 2, 3, 4, 6, 8]5 #Reverse Sort6Li = [1,3,6,4,8,2, 0]7Li.sort (reverse=True)8 Print(LI)#Output results: [8, 6, 4, 3, 2, 1, 0]9 #Reverse SortTenLi = [1,3,6,4,8,2, 0] One Li.reverse () A Print(LI)#output results: [0, 2, 8, 4, 6, 3, 1]
7. The operation iterates over the join method of the object, as well as the method of converting the list to a string, some of which can also be used instead of the for method to traverse
1 li = ['fd','dfs','fr' ,'Q2']2'*'. Join ( Li)3print(s)

List additions and deletions in Python and other related methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.