×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - List Slicing

Last Updated : May 01, 2025

In Python, list slicing allows you to extract a portion of a list by specifying a range of indices. In this chapter, we will learn how to slice lists, access sublists, and manipulate data using slicing.

Basic List Slicing

List slicing allows you to extract a portion of the list by specifying a start index, stop index, and an optional step value. The syntax for slicing is: list[start:stop:step].

Example

In this example, we are slicing the list to extract a portion of the cities list from index 1 to 3.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Slicing the list from index 1 to 3 (excluding index 3)
sliced_cities = cities[1:3]

# Displaying the sliced list
print(sliced_cities)

The output of the above code would be:

['London', 'Paris']

Slicing with Negative Indices

Negative indices can also be used in slicing to access elements from the end of the list.

Example

In this example, we are slicing the list to extract elements from the second-last to the last item.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Slicing the list using negative indices
sliced_cities = cities[-2:]

# Displaying the sliced list
print(sliced_cities)

The output of the above code would be:

['Tokyo', 'Sydney']

Slicing with a Step

The step parameter allows you to extract every n-th item from the list. If no step is provided, the default step is 1.

Example

In this example, we are using the step parameter to extract every second city from the list.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Slicing the list with a step of 2
sliced_cities = cities[::2]

# Displaying the sliced list
print(sliced_cities)

The output of the above code would be:

['New York', 'Paris', 'Sydney']

Slicing with Start, Stop, and Step

You can combine the start, stop, and step parameters to create more complex slices.

Example

In this example, we are slicing the list from index 1 to 4 with a step of 2.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Slicing the list with start, stop, and step
sliced_cities = cities[1:4:2]

# Displaying the sliced list
print(sliced_cities)

The output of the above code would be:

['London', 'Tokyo']

Slicing to Reverse a List

You can use list slicing to reverse a list by using a step of -1.

Example

In this example, we are reversing the list of cities.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Reversing the list using slicing
reversed_cities = cities[::-1]

# Displaying the reversed list
print(reversed_cities)

The output of the above code would be:

['Sydney', 'Tokyo', 'Paris', 'London', 'New York']

Exercise

Select the correct option to complete each statement about list slicing in Python.

  1. What does the expression my_list[1:4] return if my_list = [10, 20, 30, 40, 50]?
  2. What will my_list[:3] return if my_list = [5, 10, 15, 20, 25]?
  3. Given my_list = ['a', 'b', 'c', 'd', 'e'], what will my_list[::2] return?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.