添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱喝酒的四季豆  ·  So lost·  10 小时前    · 
精明的小马驹  ·  How to install pyQt5 ...·  10 小时前    · 
完美的红金鱼  ·  PyQt5 ...·  10 小时前    · 
干练的充电器  ·  Demo for core ...·  2 月前    · 
痴情的大象  ·  新闻战线·  3 月前    · 

Deque (Doubly Ended Queue) in Python is implemented using the module “ collections “. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to a list that provides O(n) time complexity.

Types of Restricted Deque Input

  • Input Restricted Deque :  Input is limited at one end while deletion is permitted at both ends.
  • Output Restricted Deque : output is limited at one end but insertion is permitted at both ends.
  • Example: Python code to demonstrate deque

    Python3

    Example 1: Appending Items Efficiently

  • append() :- This function is used to insert the value in its argument to the right end of the deque.
  • appendleft() :- This function is used to insert the value in its argument to the left end of the deque.
  • Python3

    # using append() to insert element at right end
    # inserts 4 at the end of deque
    de.append( 4 )
    # printing modified deque
    print ( "\nThe deque after appending at right is : " )
    print (de)
    # using appendleft() to insert element at left end
    # inserts 6 at the beginning of deque
    de.appendleft( 6 )
    # printing modified deque
    print ( "\nThe deque after appending at left is : " )
    print (de)

    Example 2: Popping Items Efficiently

  • pop() :- This function is used to delete an argument from the right end of the deque.
  • popleft() :- This function is used to delete an argument from the left end of the deque.
  • Python3

    # initializing deque
    de = collections.deque([ 6 , 1 , 2 , 3 , 4 ])
    print ( "deque: " , de)
    # using pop() to delete element from right end
    # deletes 4 from the right end of deque
    de.pop()
    # printing modified deque
    print ( "\nThe deque after deleting from right is : " )
    print (de)
    # using popleft() to delete element from left end
    # deletes 6 from the left end of deque
    de.popleft()
    # printing modified deque
    print ( "\nThe deque after deleting from left is : " )
    print (de)

    Example 3: Accessing Items in a deque

  • index(ele, beg, end) :- This function returns the first index of the value mentioned in arguments, starting searching from beg till end index.
  • insert(i, a) :- This function inserts the value mentioned in arguments(a) at index(i) specified in arguments.
  • remove() :- This function removes the first occurrence of the value mentioned in arguments.
  • count() :- This function counts the number of occurrences of value mentioned in arguments.
  • Python3

    de = collections.deque([ 1 , 2 , 3 , 3 , 4 , 2 , 4 ])
    # using index() to print the first occurrence of 4
    print ( "The number 4 first occurs at a position : " )
    print (de.index( 4 , 2 , 5 ))
    # using insert() to insert the value 3 at 5th position
    de.insert( 4 , 3 )
    # printing modified deque
    print ( "The deque after inserting 3 at 5th position is : " )
    print (de)
    # using count() to count the occurrences of 3
    print ( "The count of 3 in deque is : " )
    print (de.count( 3 ))
    # using remove() to remove the first occurrence of 3
    de.remove( 3 )
    # printing modified deque
    print ( "The deque after deleting first occurrence of 3 is : " )
    print (de)
    The number 4 first occurs at a position : 
    The deque after inserting 3 at 5th position is : 
    deque([1, 2, 3, 3, 3, 4, 2, 4])
    The count of 3 in deque is : 
    The deque after deleting first occurrence of 3 is : 
    deque([1, 2, 3, 3, 4, 2, 4])

    Refer end for complexity analysis.

    Example 4: Size of a deque

  • len(dequeue) :- Return the current size of the dequeue.
  • Python3

    # Python Program to demonstrate
    # how to find size of a Dequeue
    from collections import deque
    # initializing deque
    de = deque([ 1 , 2 , 3 , 4 , 5 , 6 ])
    print ( "Current Deque: " , de)
    # printing current size of deque
    print (f "Size of Deque: {len(de)}" )
    # using pop() to delete element from right end
    # deletes 6 from the right end of deque
    de.pop()
    # printing modified deque
    print ( "\nThe deque after deleting from right is: " , end = '')
    print (de)
    # printing current size of deque
    print (f "Size of Deque: {len(de)}" )
    # This code is contributed by Susobhan Akhuli

    Example 5: Front and Back of a deque

  • Deque[0] :- We can access the front element of the deque using indexing with de[0].
  • Deque[-1] :- We can access the back element of the deque using indexing with de[-1].
  • Python3

    # initializing deque
    de = deque([ 1 , 2 , 3 , 4 , 5 , 6 ])
    print ( "Current Deque: " , de)
    # Accessing the front element of the deque
    print ( "Front element of the deque:" , de[ 0 ])
    # Accessing the back element of the deque
    print ( "Back element of the deque:" , de[ - 1 ])
    # This code is contributed by Susobhan Akhuli
    Current Deque:  deque([1, 2, 3, 4, 5, 6])
    Front element of the deque: 1
    Back element of the deque: 6

    Refer end for complexity analysis.

    Example 6: Different operations on deque

  • extend(iterable) :- This function is used to add multiple values at the right end of the deque. The argument passed is iterable.
  • extendleft(iterable) :- This function is used to add multiple values at the left end of the deque. The argument passed is iterable. Order is reversed as a result of left appends.
  • reverse() :- This function is used to reverse the order of deque elements.
  • rotate() :- This function rotates the deque by the number specified in arguments. If the number specified is negative, rotation occurs to the left. Else rotation is to right.
  • Python3

    # extend(), extendleft(), rotate(), reverse()
    # importing "collections" for deque operations
    import collections
    # initializing deque
    de = collections.deque([ 1 , 2 , 3 ,])
    # using extend() to add numbers to right end
    # adds 4,5,6 to right end
    de.extend([ 4 , 5 , 6 ])
    # printing modified deque
    print ( "The deque after extending deque at end is : " )
    print (de)
    # using extendleft() to add numbers to left end
    # adds 7,8,9 to left end
    de.extendleft([ 7 , 8 , 9 ])
    # printing modified deque
    print ( "The deque after extending deque at beginning is : " )
    print (de)
    # using rotate() to rotate the deque
    # rotates by 3 to left
    de.rotate( - 3 )
    # printing modified deque
    print ( "The deque after rotating deque is : " )
    print (de)
    # using reverse() to reverse the deque
    de.reverse()
    # printing modified deque
    print ( "The deque after reversing deque is : " )
    print (de)
    The deque after extending deque at end is : 
    deque([1, 2, 3, 4, 5, 6])
    The deque after extending deque at beginning is : 
    deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
    The deque after rotating deque is : 
    deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
    The deque after reversing deque is : 
    deque([7, 8, 9, 6, 5, 4, 3, 2, 1])

    Refer end for complexity analysis.

    Complexity Analysis:

    deque::at() and deque::swap() in C++ STL
    Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
    deque::clear() and deque::erase() in C++ STL
    Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
    deque::operator= and deque::operator[] in C++ STL
    Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
    Difference between Queue and Deque (Queue vs. Deque)
    Queue: The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue. [caption width="800"]Queue Data structure[/caption]The operations allowed in the queue are:insert an element at the reardelete element from the frontget
    Deque::front() and deque::back() in C++ STL
    Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
    How to get the first and last elements of Deque in Python?
    Deque is a Double Ended Queue which is implemented using the collections module in Python. Let us see how can we get the first and the last value in a Deque. Method 1: Accessing the elements by their index. The deque data structure from the collections module does not have a peek method, but similar results can be achieved by fetching the elements
    Python - Queue.LIFOQueue vs Collections.Deque
    Both LIFOQueue and Deque can be used using in-built modules Queue and Collections in Python, both of them are data structures and are widely used, but for different purposes. In this article, we will consider the difference between both Queue.LIFOQueue and Collections.Deque concerning usability, execution time, working, implementation, etc. in Pyth
    Difference between queue.queue vs collections.deque in Python
    Both queue.queue and collections.deque commands give an idea about queues in general to the reader but, both have a very different application hence shouldn't be confused as one. Although they are different and used for very different purposes they are in a way linked to each other in terms of complete functionality. Before we jump into what they a
    How to check if a deque is empty in Python?
    In this article, we are going to know how to check if a deque is empty in Python or not. Python collection module provides various types of data structures in Python which include the deque data structure which we used in this article. This data structure can be used as a queue and stack both because it allows the user to insert and delete the elem
    IndexError: pop from Empty Deque in Python
    In Python, the IndexError: pop from an empty deque is an error that occurs when trying to use the pop() method on an empty deque. A deque (double-ended queue) is a versatile data structure that allows efficient addition and removal of elements from both ends. This article explores the causes of the error, provides examples of its occurrence, and of
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.