Slicing operations in Python

Source: Internet
Author: User

First, the list is sliced

"Forward Slice"

1, L[0:9] from the index is the element 0 interception, until the index is 9 stop, but does not include index 9.


If the starting index is 0, you can omit:


2, L[1:9] can also start from any index


3, L[1:] End index omitted, indicated until the end

4, l[:] Start index and end index at the same time omitted, means to copy a list out


5, L[1:10:2] The last 2 means that every two intercept a



"Reverse slices"

The index of the penultimate element is-1, again, the reverse slice contains the start index, and does not contain the end index.

1. Omit Start index



2. Omit End Index


3, start index, end index are not omitted


4, plus the interval



Second, slicing the tuple and string is exactly the same as list.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Slicing operations in Python

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.