Python Basics-List article

Source: Internet
Author: User

1. Modifying elements

To modify a list element, you specify the list name and the index of the element that you want to modify, and then specify the new value for the element.

1 list1 = [0,1,2,3,4,5]2print(list1)34 list1[0] = 1 # the first element will be modified to 1 5 Print (List1)
2. Add element 2.1 append ()

Adds an element to the end of the list.

1 list1 = [0,1,2,3,4,5]2print(list1)34 list1.append (6) #  Add an element at the end with a value of 65print(list1)
2.2 Insert ()

Use the method Insert () to add a new element anywhere in the list, but you need to specify the index and value of the new element.

1 list1 = [0,1,2,3,4,5]2print(list1)34# The first 6 is the index, and the second 6 is the value.  5print(list1)
3. Delete Element 3.1 del

Using del to delete an element, you need to know the index of the element.

1 list1 = [0,1,2,3,4,5]2print(list1)34del LIST1[5] #  Delete Sixth element 5print(list1)
3.2 Pop ()

With pop () , the last element is deleted by default, and any element can be deleted, but the index of the element needs to be specified.

1 list1 = [0,1,2,3,4,5]2print(list1)34# Delete the last element by default 5 Print (List1) 6 7 # Delete First element 8 Print (List1)
3.3 Remove ()

Deletes an element based on the value of the element.

1 list1 = [0,1,2,3,4,5]2print(list1)34# Remove the 0 value from the list 5 Print (List1)
4. List Sort 4.1 permanent sort
    • Permanently modifies the order in which the list elements are arranged.
    • Sort by numeric order, alphabetical order, and phonetic order.
    • If there are numbers, words, and Chinese, the order is sorted by number-word-Chinese.
    • Parameter reverse=true can be passed to the sort () method, sorted in reverse order.
    • If you want to sort the list, it can only be a string or an integer or a floating-point number, not mixed together, will be error
1List1 = ['1','EIP','Bip','CIP','AIP','Dip','0','Sample','Ah']2 3List1.sort ()#the normal order4 Print(List1)5 6List1.sort (Reverse=true)#in reverse order7 Print(List1)8 9 Print(List1)#the order of the list is not the order of the original list, it has been permanently changed
4.2 Temporary sort

To preserve the original order of the list elements, render them in a specific order, or pass the parameter reverse=true , sorted in reverse order.

1List1 = ['1','EIP','Bip','CIP','AIP','Dip','0','Sample','Ah']2 Print(List1)3 4 Print(Sorted (List1))#the normal order5 6 Print(Sorted (list1,reverse=true))#in reverse order7 8 Print(List1)#or the order of the original list
5. Flip the list

Method Reverse () permanently modifies the order in which the list elements are arranged, but can revert to the original order at any time, just call reverse () again on the list.

1 list1 = [0,1,2,3,4,5]2print(list1)34# Flip List 5 Print (List1) 6 7 # Restore List Order 8 Print (List1)
6. List length

Use the function len () to quickly learn the length of the list.

1 list1 = [0,1,2,3,4,5]2print(len (list1))

Python Basics-List 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.