Skip to content

Latest commit

 

History

History
844 lines (511 loc) · 8.03 KB

07-List-Slicing.md

File metadata and controls

844 lines (511 loc) · 8.03 KB

List Slicing in Python

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Introduction
  4. What is List Slicing?
  5. Why is List Slicing Required?
  6. Syntax
  7. Components of Slice
  8. Positive Indexing
  9. Negative Indexing
  10. Step Value
  11. Slice Assignment
  12. Internal Working
  13. Summary

Learning Objectives

After completing this chapter, you will be able to:

  • Understand list slicing.
  • Use start, stop, and step values.
  • Apply positive and negative indexing.
  • Reverse lists using slicing.
  • Modify lists using slice assignment.
  • Apply slicing in real-world programs.

Prerequisites

Before learning this chapter, you should know:

  • Lists
  • Indexing
  • List Comprehension
  • Operators

Introduction

Lists often contain many elements.

Sometimes,

we need only a portion of a list instead of the entire list.

Examples include:

  • First five students
  • Last three records
  • Every alternate value
  • Reverse order

Instead of using loops,

Python provides List Slicing.


What is List Slicing?

Definition

List Slicing is the process of extracting a portion of a list using index positions.


Simple Definition

List slicing creates a new list containing selected elements from an existing list.


Why is List Slicing Required?

Without slicing,

extracting part of a list requires loops.

List slicing provides a shorter and more readable solution.

Example

numbers = [10,20,30,40,50]

print(numbers[1:4])

Output

[20, 30, 40]

Syntax

list[start:stop]

Syntax with Step

list[start:stop:step]

Components of Slice

Start

The index from where slicing begins.

Included in the result.


Stop

The index where slicing ends.

Not included in the result.


Step

Determines how many positions to move between elements.

Default

1

Positive Indexing

List

A   B   C   D   E

0   1   2   3   4

Example

letters = ['A','B','C','D','E']

print(letters[1:4])

Output

['B', 'C', 'D']

Negative Indexing

List

A   B   C   D   E

-5 -4 -3 -2 -1

Example

letters = ['A','B','C','D','E']

print(letters[-4:-1])

Output

['B', 'C', 'D']

Omitting Start

If the start index is omitted,

Python begins from index

0

Example

numbers = [10,20,30,40]

print(numbers[:3])

Output

[10, 20, 30]

Omitting Stop

If the stop index is omitted,

Python continues until the last element.

Example

numbers = [10,20,30,40]

print(numbers[2:])

Output

[30, 40]

Omitting Both

Example

numbers = [10,20,30]

print(numbers[:])

Output

[10,20,30]

This creates a shallow copy of the list.


Step Value

Example

numbers = [1,2,3,4,5,6,7,8]

print(numbers[::2])

Output

[1,3,5,7]

Reverse Slicing

Using

[::-1]

Example

numbers=[1,2,3,4]

print(numbers[::-1])

Output

[4,3,2,1]

Slice Assignment

Slices can also replace existing elements.

Example

numbers=[10,20,30,40]

numbers[1:3]=[200,300]

print(numbers)

Output

[10,200,300,40]

Internal Working

Original List

↓

Read Start Index

↓

Read Stop Index

↓

Read Step

↓

Create New List

↓

Copy Selected Elements

↓

Return New List

Advantages

  • Easy extraction.
  • Cleaner code.
  • Faster than manual looping for many slicing tasks.
  • Supports reversing.
  • Supports copying.

Best Practices

  • Use meaningful index values.
  • Use slicing instead of loops for simple extraction.
  • Keep slice expressions readable.
  • Use [::-1] only when reversing is intended.
  • Understand that slicing creates a new list.

Common Mistakes

Mistake 1

Assuming the stop index is included.

Example

numbers[1:4]

Returns

1,2,3

Not

1,2,3,4

Mistake 2

Using incorrect negative indices.


Mistake 3

Using step value

0

This raises

ValueError

Mistake 4

Assuming slicing modifies the original list.

Slicing creates a new list unless used with slice assignment.


Program 1 – Basic Slicing

numbers=[10,20,30,40,50]

print(numbers[1:4])

Output

[20,30,40]

Program 2 – First Three Elements

numbers=[10,20,30,40,50]

print(numbers[:3])

Output

[10,20,30]

Program 3 – Last Three Elements

numbers=[10,20,30,40,50]

print(numbers[-3:])

Output

[30,40,50]

Program 4 – Alternate Elements

numbers=[1,2,3,4,5,6,7,8]

print(numbers[::2])

Output

[1,3,5,7]

Program 5 – Reverse List

numbers=[1,2,3,4,5]

print(numbers[::-1])

Output

[5,4,3,2,1]

Program 6 – Reverse Alternate Elements

numbers=[1,2,3,4,5,6,7,8]

print(numbers[::-2])

Output

[8,6,4,2]

Program 7 – Copy List

numbers=[10,20,30]

copy=numbers[:]

print(copy)

Output

[10,20,30]

Program 8 – Slice Assignment

numbers=[10,20,30,40]

numbers[1:3]=[200,300]

print(numbers)

Output

[10,200,300,40]

Program 9 – Delete Using Slice

numbers=[10,20,30,40,50]

del numbers[1:4]

print(numbers)

Output

[10,50]

Program 10 – Characters Using Slice

text=list("PYTHON")

print(text[1:5])

Output

['Y','T','H','O']

Dry Run

List

↓

Start Index

↓

Stop Index

↓

Step Value

↓

Selected Elements

↓

New List

↓

Output

Memory Representation

Original List

│

├──10

├──20

├──30

├──40

└──50

↓

Slice [1:4]

↓

New List

│

├──20

├──30

└──40

Advantages

  • Simple syntax.
  • Easy extraction.
  • Efficient copying.
  • Supports reversing.
  • Improves readability.

Best Practices

  • Keep slice expressions simple.
  • Use negative indexing carefully.
  • Avoid unnecessary slicing.
  • Use slice assignment only when modification is required.

Common Mistakes

  • Assuming stop index is included.
  • Using invalid step values.
  • Confusing copying with referencing.
  • Misusing negative indices.

Interview Questions

1. What is list slicing?

Answer

List slicing extracts a portion of a list using index positions.


2. What is the syntax of list slicing?

Answer

list[start:stop:step]

3. Is the stop index included?

Answer

No.

The stop index is excluded.


4. How do you reverse a list using slicing?

Answer

list[::-1]

5. Can slicing create a copy of a list?

Answer

Yes.

copy = list[:]

creates a shallow copy.


6. What happens if the start index is omitted?

Answer

Python starts from the beginning of the list.


7. What happens if the stop index is omitted?

Answer

Python continues until the end of the list.


8. Can slicing modify a list?

Answer

Slicing itself returns a new list.

However, slice assignment can modify the original list.


Practice Programs

  1. Extract the first five elements.
  2. Extract the last four elements.
  3. Print alternate elements.
  4. Reverse a list.
  5. Replace elements using slice assignment.
  6. Delete elements using slicing.

Quick Revision

  • Slice Syntax → list[start:stop:step]
  • Start → Included
  • Stop → Excluded
  • Step → Increment
  • [::-1] → Reverse
  • [:] → Copy
  • Slice Assignment → Modify original list

Chapter Summary

In this chapter, you learned:

  • List Slicing
  • Slice Syntax
  • Positive Indexing
  • Negative Indexing
  • Step Value
  • Reverse Slicing
  • Slice Assignment
  • Practical Programs
  • Advantages
  • Best Practices
  • Common Mistakes
  • Interview Questions
  • Practice Programs
  • Quick Revision