Python Tutorial

Python List Slicing Explained With Example

How to Slice a List in Python?

Slice operation is done on lists with the use of a colon(:). If you want to print elements from beginning to a range, use [: Index]. To print elements from the end, use [:-Index]. 

Alternatively, if you want to print elements from a specific Index till the end, use [Index:]. To print elements within a range, use [Start Index:End Index].

Lastly, if you want to print the whole List with the use of slicing operation, use [:]. 

For printing the whole list in reverse, use [::-1].

Example:

text = 'Python Programing'

# get slice object to slice Python

sliced_text = slice(6)

print(text[sliced_text])

Output:

Python

https://youtu.be/SkFidEd3f_0

Did you find this article helpful?