Python data Type-list-add and revise

Source: Internet
Author: User

1. Adding elements

Add a single element: Use the Append (object) function to add a single element to the list, with the argument object being the objects, meaning that all Python objects can be added to the list.

Add multiple elements (Consolidated list): Use the Extend (iterable) function to add multiple elements to the list, and the parameter iterable as an iterative object.

The following code shows the actions to add elements to the list:

Lst1 = [1,2,3,4,5,6,7,8,9] # Create List lst2 = [' A ', ' B ', ' C '] # Create List lst1. Append (10) # Add a single element to the list lst2. Extend ([' d ', ' E ']) # Add multiple elements to the list print (LST1,LST2)

After running the code, the result is:

2. Changing elements

Change a single element: list [index position] = new element

Example code:

LST = [1,2,3,4,5,6,7,8,9] # Create List lst[3]= ' x ' # Change the element of the specified position to the new element print (LST) # The above code runs the result: [1, 2, 3, ' X ', 5, 6, 7, 8, 9]

Change multiple elements: list [start position, end position] = new element

LST = [1,2,3,4,5,6,7,8,9] # Create List lst[3:7]= ' x ', ' y ' # change the element of the specified position interval to the new element, the number does not need to correspond to print (LST) # above the code run result: [1, 2, 3, ' x ', ' Y ', 8, 9]

3. Inserting elements

Insert a single element: using the Insert (index,object) function, the parameter index is the index position, indicating that a new element is inserted before that position;

LST = [1,2,3,4,5,6,7,8,9] # Create list lst. Insert (0, ' list ') # start position Insert new element, 0 indicates the first index position print (LST) # above code run Result: [' list ', 1, 2, 3, 4, 5, 6, 7, 8, 9]

Insert multiple elements: list [index position: Index position] = new element; Note that the two index locations remain consistent.

LST = [1,2,3,4,5,6,7,8,9] # Create List lst[3:3]= ' * ', ' * '  # Specify position in front of insert new element print (LST) # above code run result: [1, 2, 3, ' * ', ' * ', 4, 5, 6, 7, 8 , 9]

4. Remove the Element

Using the POP (index) function, the argument index is the index position of the element being fetched.

Example code:

LST = [1,2,3,4,5,6,7,8,9]print (LST.  Pop(5)) print (LST)

After running the code, the result is:

5. Deleting elements

Delete the specified content element: Use the Remove (object) function to remove the first occurrence of the same element in the list as the parameter, or to throw an exception if no element of the same argument exists in the list.

LST = [' You ', ' I ', ' he ', ' me ', ' you ', ' he '] # Create a list of LST. Remove (' I ') # Delete the first element that matches the specified content from left to right print (LST) # The above code runs the result: [' You ', ' he ', ' me ', ' you ', ' he ']

Delete a single specified location element: Del list [index position]

LST = [' small ', ' floor ', ' yes ', ' a ', ' handsome ', ' brother '] # Create List del lst[3] # Delete the specified position of the element print (LST) # above code run result: [' small ', ' floor ', ' yes ', ' handsome ', ' elder brother ']

Delete multiple specified location elements: Del list [start position: End position]

LST = [' small ', ' floor ', ' yes ', ' a ', ' handsome ', ' elder brother '] # Create List del lst[2:4] # Delete multiple elements from the start position to the terminating position print (LST) # above code running result: [' small ', ' floor ', ' handsome ', ' elder brother ']

Delete End element: Using the pop () function, the argument is empty.

LST = [' small ', ' floor ', ' true ', ' yes ', ' handsome ', ' brother '] # Create List lst.pop () # Delete the end of the element print (LST) # above the code run result: [' small ', ' floor ', ' true ', ' yes ', ' handsome ']

Clear all elements:

Use the clear () function.

Example code:

LST = [1,2,3,4,5,6] # Create list lst. Clear () # Empty all elements Print (LST) # The above code runs the result: []

Alternatively, use the del command: Del list [:]

LST = [1,2,3,4,5,6] # Create List del lst[:] # empty all elements Print (LST) # The above code runs the result: []

6. List sorting

Reverse sort: Use the reverse () function.

LST = [3,2,4,5,6,1] # Create List lst.reverse () # reverse Sort list element print (LST) # above code run result: [1, 6, 5, 4, 2, 3]

Ascending and Descending sort: using the sort (cmp,key,reverse) function, the parameter CMP is a function, the parameter key is a function, and the reverse is a Boolean (True and false).

When the parameter is empty, it is sorted in ascending order by default.

Example code:

LST = [6,1,2,3,4,5] # Create List lst.sort () # Ascending sort list element print (LST) # above code run result: [1, 2, 3, 4, 5, 6]

Convert to descending order by setting the parameter reverse=true.

LST = [6,1,2,3,4,5] # Create List lst.sort (reverse=true) # descending sort list element print (LST) # above code run result: [6, 5, 4, 3, 2, 1]

The ascending order can also use the function sorted (iterable,cmp,key,reverse), the parameter iterable is an iterative object, the parameter CMP is a function, the parameter key is a function, and the reverse is a Boolean value.

The sorted () function does not change the original list.

LST = [3,2,4,5,6,1] # Create List print (sorted(LST)) # Output display ascending sequence table print (sorted (lst,reverse=true)) # Output display descending sequence table print (LST) # Output shows the original list # above code run result: # [1, 2, 3, 4, 5, 6]# [6, 5, 4, 3, 2, 1]# [3, 2, 4, 5, 6, 1]

7. Convert tuples to lists

Using the list (iterable) function, the parameter iterable is an iterative object.

Tup = (3,2,4,5,6,1) # Create List LST = List (tup) print (LST) # above code run result: [3, 2, 4, 5, 6, 1]

Python data Type-list-add and revise

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.