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
print(Lst[::2])
Output:
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']
print(L[2:-5])
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.