14 Basics of Python programming

Source: Internet
Author: User

Added elements of the list

L1 = [10, 20, 30]

L1.append (40) # appends an element with a value of 40 to the end

L1.extend ([50, 60]) # Appends a series of elements to the end, extend + iterates over an object

L1.insert (Len (L1), 70) # Inserts an element in the set subscript position, before the specified subscript

List of deleted elements

m = L1.pop () # Deletes an element without an input parameter, which defaults to deleting the last one and returning the deleted element value

Print (m) # 70

L1.pop (0) # Delete an element with the subscript 0 position

L1.remove (10) # remove elements with an element value of 10

L1.remover (1000) # If this value is not true, an error will be ValueError:list.remove (x): X not in List

L1.clear () # Empty list

L1 = [10, 20, 30]

List reversal

L1.reverse ()

L1 = [90, 30, 70, 20, 10, 60]

List sort

The list name. Sort (key,reverse) key indicates which function to sort by, whether the reverse is large or small

L1.sort (reverse = true) # from big to small

L1.sort (reverse = False) # Small to large

L1.sort () # No input parameters, default from small to large

Built-in function sorted (key,reverse)

L1 = sorted (L1, Reverse=true)

14 Basics of Python programming

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.