{"id":25915,"date":"2021-12-03T21:29:18","date_gmt":"2021-12-03T15:59:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=25915"},"modified":"2021-12-03T21:29:18","modified_gmt":"2021-12-03T15:59:18","slug":"python-collections-deque-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-collections-deque-method-with-examples\/","title":{"rendered":"Python collections Deque() Method with Examples"},"content":{"rendered":"
Deque:<\/strong><\/p>\n In Python, the module “collections” is used to implement a Deque (Double Ended Queue). Deque is chosen over list when we need faster append and pop operations from both ends of the container, as deque has an O(1) time complexity for append and pop operations, whereas list has an O(n) time complexity.<\/p>\n Access Operations on Deque():<\/strong><\/p>\n append():<\/strong><\/p>\n appendleft():<\/strong><\/p>\n pop():<\/strong><\/p>\n popleft():<\/strong><\/p>\n index(ele, beg, end):<\/strong><\/p>\n insert(i, a):<\/strong><\/p>\n remove():<\/strong><\/p>\n count():<\/strong><\/p>\n extend(iterable):<\/strong><\/p>\n extendleft(iterable):<\/strong><\/p>\n reverse():<\/strong><\/p>\n rotate():<\/strong><\/p>\n 1)append(), appendleft(), pop() ,popleft() Operations on deque<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)index(), insert(), count() ,remove() Operations on deque<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3)extend(), extendleft(), rotate() ,reverse() Operations on deque<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Deque: In Python, the module “collections” is used to implement a Deque (Double Ended Queue). Deque is chosen over list when we need faster append and pop operations from both ends of the container, as deque has an O(1) time complexity for append and pop operations, whereas list has an O(n) time complexity. Access Operations …<\/p>\nappend() function adds the value in its argument to the right end of the \r\ndeque.<\/pre>\n
appendleft() function inserts the value in its argument to the left end of\r\n the deque.<\/pre>\n
pop() function is used to remove an argument from the deque's right end.<\/pre>\n
popleft() function is used to remove an argument from the deque's left end.<\/pre>\n
This method returns the first index of the value specified in parameters,\r\n beginning with beg and ending with end index.<\/pre>\n
Inserts the value specified in arguments(a) at the index(i) specified in\r\narguments.<\/pre>\n
This function deletes the first occurrence of the value specified in the \r\nparameters.<\/pre>\n
This function counts the number of times the value specified in arguments \r\nappears.\r\n<\/pre>\n
This function adds several values to the right end of a deque. \r\nThe passed argument is iterable.\r\n<\/pre>\n
This function is used to add numerous values to the deque's left end. \r\nThe passed argument is iterable. As a result of left appends, the order is \r\nreversed.\r\n<\/pre>\n
This function reverses the order of deque elements.\r\n<\/pre>\n
This function rotates the deque by the number of arguments supplied. \r\nIf the provided integer is negative, the rotation is to the left. Otherwise, \r\nrotate to the right.<\/pre>\n
collections Deque() Method with Examples in Python<\/h2>\n
\n
# Import collections module using the import keyword\r\nimport collections\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [5, 6, 7]\r\n# Pass the given list as an argument to the deque() function for initializing the\r\n# deque and store it in another variable.\r\ndequ = collections.deque(gvn_lst)\r\n# Append some random element to the deque using the append() function to insert an\r\n# element at the right end of a deque.\r\ndequ.append(8)\r\n# Print the deque after appending right.\r\nprint(\"After appending at the right, the deque = \")\r\nprint(dequ)\r\n# Append left some random element to the deque using the appendleft() function to\r\n# insert an element at the left end of a deque. It inserts an element at\r\n# the beginning.\r\ndequ.appendleft(1)\r\n# Print the deque after appending left.\r\nprint(\"After appending at the left, the deque = \")\r\nprint(dequ)\r\n# Apply pop() method to remove an element from the right end of the deque.\r\ndequ.pop()\r\n# Print the deque after poping.\r\nprint(\"After deleting from the right, the deque = \")\r\nprint(dequ)\r\n# Apply popleft() method to remove an element from the left end of the deque.\r\ndequ.popleft()\r\n# Print the deque after applying popleft() function.\r\nprint(\"After deleting from the left, the deque = \")\r\nprint(dequ)\r\n<\/pre>\n
After appending at the right, the deque = \r\ndeque([5, 6, 7, 8])\r\nAfter appending at the left, the deque = \r\ndeque([1, 5, 6, 7, 8])\r\nAfter deleting from the right, the deque = \r\ndeque([1, 5, 6, 7])\r\nAfter deleting from the left, the deque = \r\ndeque([5, 6, 7])<\/pre>\n
\n
# Import collections module using the import keyword\r\nimport collections\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [5, 6, 5, 7, 6, 5, 8, 3]\r\n# Pass the given list as an argument to the deque() function for initializing the\r\n# deque and store it in another variable.\r\ndequ = collections.deque(gvn_lst)\r\n# Print the index of first occurrence of an element using the index() method by passing\r\n# the element, starting and ending value as the arguments.\r\nprint(\"The index of first occurrence of 5 = \")\r\nprint(dequ.index(5, 1, 6))\r\n# Pass the position and some random value and as the arguments to the insert() method\r\n# to insert an element at the specified position.\r\ndequ.insert(5, 9)\r\n# Print the deque after inserting.\r\nprint(\"After inserting 9 in the 6th position the deque becomes: \")\r\nprint(dequ)\r\n# Count the frequency of an element using the count() method by passing the value\r\n# as an argument and print it.\r\nprint(\"In deque, the frequency of 5 = \")\r\nprint(dequ.count(5))\r\n# Remove the first occurrence of an element using the remove() method by passing\r\n# the element as an argument.\r\ndequ.remove(6)\r\n# Print the deque after removing the first occurrence of a specified element.\r\nprint(\"After removing the first occurrence of 6, the deque = \")\r\nprint(dequ)\r\n<\/pre>\n
The index of first occurrence of 5 = \r\n2\r\nAfter inserting 9 in the 6th position the deque becomes: \r\ndeque([5, 6, 5, 7, 6, 9, 5, 8, 3])\r\nIn deque, the frequency of 5 = \r\n3\r\nAfter removing the first occurrence of 6, the deque = \r\ndeque([5, 5, 7, 6, 9, 5, 8, 3])<\/pre>\n
\n
# Import collections module using the import keyword\r\nimport collections\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [5, 6, 7]\r\n# Pass the given list as an argument to the deque() function for initializing the\r\n# deque and store it in another variable.\r\ndequ = collections.deque(gvn_lst)\r\n# Add some random numbers to the right end to the deque using the extend()\r\n# method by passing the list as an argument.\r\ndequ.extend([8, 9, 10])\r\n# Print the deque after extending to the right end.\r\nprint(\"After extending to the right end, the deque = \")\r\nprint(dequ)\r\n# Add some random numbers to the left end to the deque using the extendleft()\r\n# method by passing the list as an argument.\r\ndequ.extendleft([11, 12, 13])\r\n# Print the deque after extending to the left end.\r\nprint(\"After extending to the left end(beginning), the deque = \")\r\nprint(dequ)\r\n# Rotate the deque by left\/right using the rotate() method by passing some value as\r\n# an argument.\r\n# The deque rotates by 2 to the left.\r\ndequ.rotate(-2)\r\n# Print the deque after rotation by 2 to the left.\r\nprint(\"After rotation by 2 to the left, the deque = \")\r\nprint(dequ)\r\n# Reverse the deque by using the reverse() method.\r\ndequ.reverse()\r\n# Print the deque after reversing.\r\nprint(\"After reversing, the deque = \")\r\nprint(dequ)\r\n<\/pre>\n
After extending to the right end, the deque = \r\ndeque([5, 6, 7, 8, 9, 10])\r\nAfter extending to the left end(beginning), the deque = \r\ndeque([13, 12, 11, 5, 6, 7, 8, 9, 10])\r\nAfter rotation by 2 to the left, the deque = \r\ndeque([11, 5, 6, 7, 8, 9, 10, 13, 12])\r\nAfter reversing, the deque = \r\ndeque([12, 13, 10, 9, 8, 7, 6, 5, 11])<\/pre>\n