Python

 

 

Introduction

In Python, the collections.deque is a powerful data structure that allows fast appending and popping from both ends. It’s part of the collections module and offers a more efficient alternative to lists when it comes to operations that involve frequent insertions or deletions from the ends of the collection.

Objective

This guide aims to help you understand how to use collections.deque for handling data efficiently. By the end of this tutorial, you should be able to use deque for applications like queue management, sliding window problems, and more.

Code Example

from collections import deque

# Create an empty deque
dq = deque()

# Add elements to the right side of the deque
dq.append(10)
dq.append(20)
dq.append(30)
print("Deque after append:", dq)

# Add elements to the left side of the deque
dq.appendleft(5)
dq.appendleft(1)
print("Deque after appendleft:", dq)

# Pop elements from the right side
right_pop = dq.pop()
print("Popped from right:", right_pop)

# Pop elements from the left side
left_pop = dq.popleft()
print("Popped from left:", left_pop)

# Rotate the deque
dq.rotate(2)
print("Deque after rotating by 2:", dq)

# Access the deque
print("First element:", dq[0])
print("Last element:", dq[-1])

Program Explanation

In this Python program, we:

  • Import the deque class from the collections module.
  • Initialize an empty deque and add elements to both ends using append and appendleft methods.
  • Use the pop and popleft methods to remove elements from the right and left ends, respectively.
  • Demonstrate the use of rotate to shift all elements of the deque by a specified number of positions.
  • Access elements using indexing ([0] for the first and [-1] for the last element).

How to Run the Program

To run this program:

  1. Ensure you have Python installed on your system (Python 3.x recommended).
  2. Copy the provided code into a Python script (e.g., deque_example.py).
  3. Open a terminal or command prompt and navigate to the directory containing the script.
  4. Run the script by typing python deque_example.py in your terminal.

The output will be printed on the console, showing the manipulation of the deque with various operations.

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)