(self-hing ai) pythony list

Source: Internet
Author: User

How to create a list:

a=[1,2,3,4,5]

Use a bracket like this to enclose an element in a comma-delimited list of elements, which is represented as a listing.

Common actions in the list:

1. Element Assignment:

a[0]=10

Added a 10 to the list index of 0, overwriting the original 1.

Results: a=[10,2,3,4,5]

2. Add elements:

A.append (6)

Here we use a method append (), the Append () method to add a new object at the end of the list.

Results: a=[10,2,3,4,5,6]

3. Delete the element:

Del A[0]

Here, we used the Del to delete the element with List index 0.

Results: a=[2,3,4,5,6]

Common methods in the list:

1.append:

A.append (7)# Add a new object at the end of the list

2.count:

A.count (2)# used to count the number of occurrences of 2 in List a

3.pop:

A.pop ()# removes the last element in the list by default and returns the value of the element A.pop (3)# removes the element at the specified position and returns the value of the  element

4.insert:

A.insert (2,10)  # Inserts a 10 at the index value of 2, the element on index 2 and the subsequent element index plus 1

5.remove:

A.remove# Remove the first match starting at index 0

6.sort:

A.sort ()# default ascending order

Use the reverse parameter:

A.sort (reverse=true)# reverse order

Strings can use Key=len:

A.sore (Key=len)# Sort by string by short to long A.sore (key=len,reverse=true)# Sort by string from long to short

(self-hing ai) pythony list

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.