{"id":180,"date":"2026-09-03T08:59:07","date_gmt":"2026-09-03T08:59:07","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=180"},"modified":"2026-09-03T08:59:07","modified_gmt":"2026-09-03T08:59:07","slug":"write-a-python-code-to-implement-double-ended-queue","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/write-a-python-code-to-implement-double-ended-queue\/","title":{"rendered":"Write a Python code to implement Double Ended Queue."},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Deque:\n    \"\"\"\n    Implementation of a Double Ended Queue (Deque) using a Python list.\n    \"\"\"\n    def __init__(self):\n        # Initialize an empty list to store deque elements\n        self.items = []\n\n    def is_empty(self):\n        \"\"\"Check if the deque is empty.\"\"\"\n        return len(self.items) == 0\n\n    def add_front(self, item):\n        \"\"\"Add an element to the front of the deque.\"\"\"\n        self.items.insert(0, item)\n        print(f\"Added '{item}' to the front.\")\n\n    def add_rear(self, item):\n        \"\"\"Add an element to the rear of the deque.\"\"\"\n        self.items.append(item)\n        print(f\"Added '{item}' to the rear.\")\n\n    def remove_front(self):\n        \"\"\"Remove and return an element from the front of the deque.\"\"\"\n        if self.is_empty():\n            return \"Deque is empty. Cannot remove from front.\"\n        item = self.items.pop(0)\n        print(f\"Removed '{item}' from the front.\")\n        return item\n\n    def remove_rear(self):\n        \"\"\"Remove and return an element from the rear of the deque.\"\"\"\n        if self.is_empty():\n            return \"Deque is empty. Cannot remove from rear.\"\n        item = self.items.pop()\n        print(f\"Removed '{item}' from the rear.\")\n        return item\n\n    def peek_front(self):\n        \"\"\"Return the element at the front without removing it.\"\"\"\n        if self.is_empty():\n            return \"Deque is empty.\"\n        return self.items[0]\n\n    def peek_rear(self):\n        \"\"\"Return the element at the rear without removing it.\"\"\"\n        if self.is_empty():\n            return \"Deque is empty.\"\n        return self.items[-1]\n\n    def size(self):\n        \"\"\"Return the number of elements in the deque.\"\"\"\n        return len(self.items)\n\n    def display(self):\n        \"\"\"Display all elements in the deque.\"\"\"\n        if self.is_empty():\n            print(\"Deque is empty.\")\n        else:\n            print(\"Deque contents (Front -&gt; Rear):\", self.items)\n\n# --- Example Usage (Managing a list of Indian cities) ---\ndeque = Deque()\n\nprint(\"--- Adding elements ---\")\ndeque.add_rear(\"Mumbai\")\ndeque.add_rear(\"Delhi\")\ndeque.add_front(\"Chennai\")\ndeque.add_front(\"Kolkata\")\ndeque.display()\n\nprint(\"\\n--- Removing elements ---\")\ndeque.remove_front()\ndeque.remove_rear()\ndeque.display()\n\nprint(\"\\n--- Peeking elements ---\")\nprint(f\"Front element: {deque.peek_front()}\")\nprint(f\"Rear element: {deque.peek_rear()}\")\n\nprint(f\"\\nCurrent size: {deque.size()}\")\n\nprint(\"\\n--- Removing all elements ---\")\ndeque.remove_front()\ndeque.remove_front()\ndeque.display()\nprint(f\"Is deque empty? {deque.is_empty()}\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>class Deque: &#8220;&#8221;&#8221; Implementation of a Double Ended Queue (Deque) using a Python list. &#8220;&#8221;&#8221; def __init__(self): # Initialize an empty list to store deque elements self.items = [] def is_empty(self): &#8220;&#8221;&#8221;Check if the deque is empty.&#8221;&#8221;&#8221; return len(self.items) == 0 def add_front(self, item): &#8220;&#8221;&#8221;Add an element to the front of the deque.&#8221;&#8221;&#8221; self.items.insert(0, item) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[53,25,26,45],"class_list":["post-180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aidpd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions\/181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}