Skip to content

Slicing a list in Python | Example code

  • by

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 or suggestions on this Python slicing topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Your email address will not be published. Required fields are marked *