Slicing a list in Python | Example code

Use the simple slicing operator i.e. colon(:) to Slicing a list in Python. The below expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

Lsit[ Initial : End : IndexJump ]

Read: Slice notation

Example Slicing a list in Python

Simple example code. Get the sliced list of every other element.

# Initialize list
Lst = [50, 70, 30, 20, 90, 10, 50]

# Display list


Slicing a list in Python

Slice with Negative Indices

You can specify both positive and negative indices at the same time.

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Output: [‘c’, ‘d’]

Do comment if you have any doubts and suggestions on this Python slicing topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.