B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python List Slicing | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Python List Slicing

Slicing helps you access a specific part of a Python list using indexes. Syntax is list[start:stop:step].

Slicing से आप Python list के किसी हिस्से को index के जरिए access कर सकते हैं। इसका syntax है list[start:stop:step].

1. Slice from index 1 to 3
names = ["abhishek", "badri", "devendra", "chandrakant"]
print(names[1:3])
Output:
['badri', 'devendra']
2. Slice from beginning to index 2
print(names[:2])
Output:
['abhishek', 'badri']
3. Slice from index 2 to end
print(names[2:])
Output:
['devendra', 'chandrakant']
4. Slice entire list
print(names[:])
Output:
['abhishek', 'badri', 'devendra', 'chandrakant']
5. Slice with step 2
print(names[::2])
Output:
['abhishek', 'devendra']
6. Slice with negative indexes
print(names[-3:-1])
Output:
['badri', 'devendra']
7. Reverse the list using slicing
print(names[::-1])
Output:
['chandrakant', 'devendra', 'badri', 'abhishek']