List Slicing

Similar to list indexing, you can use list slicing to get portions of a list

The general syntax is:

list_name[start:stop:step]  # step is optional

For example:

my_list = [1, 2, "oh no", 4, 5.62]
print(my_list[1:3])  # prints [2, "oh no"]

View code on GitHub.

Some tips for using this:

You can also slice a list using negative indexes:

my_list = [1, 2, "oh no", 4, 5.62]
print(my_list[-1:-3:-1])  # prints [5.62, 4] 

Previous Section

6.2 List Indexing

Next Section

6.4 Manipulating Lists

<aside> ⚖️ Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License.

If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact [email protected] for inquiries.

</aside>