Programmer's Python Data - Stacks, Queues and Deques
Written by Mike James   
Monday, 27 May 2024
Article Index
Programmer's Python Data - Stacks, Queues and Deques
The Queue
circular buffer

The deque is implemented as a doubly-linked list and this makes it fast, i.e. O(1) or constant time, to work at the right and left ends as there are pointers to each end. Away from the ends accessing an element involves following pointers from the initial element to the target element and takes longer, i.e. O(n) or the time increases with n, where n is the length of the deque.

By default the deque is created in an unlimited form and will grow as you add elements. If you specify maxlen in the constructor the deque is limited to that number of elements. The deque will grow to maxlen elements and if you add more the same number of elements are removed from the other end. This allows the deque to be used as a circular buffer where elements are added at the front and lost from the back if the number of elements exceeds the specified size limit, for example:

from collections import deque
circ = deque(maxlen=3)
circ.append("A")
circ.append("B")
circ.append("C")
circ.append("D")
print(circ)

In this case the deque is limited to three elements. After adding A, B and C adding D causes A to “drop off” the end of the deque and hence the print displays:

deque(['B', 'C', 'D'], maxlen=3)

Typically a circular buffer is used when you can’t process data as fast as it comes in and have to abandon items that have been waiting too long. Depending on which end of the circular buffer you take elements from, you can choose to prioritize either the elements that have been waiting longest or those that have just arrived. You can also use the rotate method to get any element of the deque to the left- or right-hand end where it can be popped, i.e. removed.

In chapter but not in this extract

  • Named Tuples
  • Counter
  • Binary Tree
  • Complete Listing

Summary

  • The LIFO stack is a fundamental data structure and it can be implemented using a Python list.

  • The key feature of a LIFO stack is that it reverses the order of items added to it.

  • The queue, or FIFO stack, is also fundamental and it too can be implemented using a Python list, or more efficiently using a Python deque.

  • A queue does not change the order of items added to it.

  • A double-ended queue or deque is implemented using the Python deque.

  • A deque has characteristics of a LIFO and a FIFO stack.

  • Named tuples are similar to structs or records in other languages.

  • Named tuples are immutable.

  • The Counter is similar to what other languages call a bag or a multiset. It works like a dictionary or set, but stores the count of the number of times each element has been added.

  • Python does not have a standard binary tree data structure. However, adding one is easy and an excellent example of using stacks and queues.

Programmer's Python
Everything is Data

Is now available as a print book: Amazon

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
       Extract Stacks, Queues and Deques
      
    Extract Named Tuples and Counters***NEW!!!
  12. Bits & Bit Manipulation
       Extract Bits and BigNum 
  13. Bytes
       Extract Bytes And Strings
       Extract Byte Manipulation 
  14. Binary Files
  15. Text Files
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. Python and Native Code
        Extract   Native Code
    Appendix I Python in Visual Studio Code
    Appendix II C Programming Using Visual Studio Code

<ASIN:1871962765>

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:B0CK71TQ17>

<ASIN:187196265X>

Related Articles

Creating The Python UI With Tkinter

Creating The Python UI With Tkinter - The Canvas Widget

The Python Dictionary

Arrays in Python

Advanced Python Arrays - Introducing NumPy

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Monday, 27 May 2024 )