What is List Slicing in Python | EP-19 List Slicing in Python | How to Slice Lists in Python

preview_player
Показать описание
Slicing of a List

We can get substrings and sublists using a slice. In Python List, there are multiple ways to print the whole list with all the elements, but we use the Slice operation to print a specific range of elements from the list. 

Slice operation is performed on Lists using a colon(:). 

To print elements from beginning to a range, use:

[: Index]

To print elements from the beginning to the negative range, use the following:

[:-Index]

To print elements from a specific Index till the end, use 

[Index:]

To print elements from a specific negative Index till the end, use

[-Index:]

To print the whole list in reverse order, use 

[::-1]

Note – To print elements of the List from the rear end, use Negative Indexes. 

Python program to demonstrate
Removal of elements in a List

Creating a List
company = ['c','y','b','r','o','s','y','s']
print(company)
print(len(company))

Printing elements from
beginning till end
print(company[:])
print(company[::])​

Print elements of a range
using Slice operationprint(company[3:8])

Print elements from a
pre-defined point to end
print(company[5:])

Negative index List slicing

Print elements from beginning
to a pre-defined point using Slice
print(company[:-6])

Print elements of a range
using negative index List slicing
print(company[-6:-1])

Printing elements in reverse
using Slice operation
print(company[::-1])

#python #list #listslicing #pythonprogramming #coding #programming #tutorial #learntocode #datascience #pythontips #python3 #pythontutorial #pythoncode #pythondeveloper #pythonista #LearnPython #CodingForBeginners #TechEducation

Connect With Us:
—————————————
Рекомендации по теме