Skip to Content
Secret Recipes of the Python Ninja
book

Secret Recipes of the Python Ninja

by Cody Jackson
May 2018
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 37m
English
Packt Publishing
Content preview from Secret Recipes of the Python Ninja

How to do it...

Let's walk through an example from https://docs.python.org/3/library/collections.html#collections.deque:

  1. Import deque from the collections module:
      >>> from collections import deque
  1. Create a deque object. In this case, we will give it a string object as an argument:
      >>> d = deque("ghi")
  1. Simple iteration over the string:
      >>> for elem in d:       ...     print(elem.upper())      G      H      I
  1. Add additional items to the front and rear of the deque:
      >>> d.append('j') # add a new entry to the right side      >>> d.appendleft('f') # add a new entry to the left side
  1. Show the new deque object:
      >>> d # show the representation of the deque      deque(['f', 'g', 'h', 'i', 'j'])
  1. Pop out the left- and right-most elements:
      >>> d.pop()       'j' >>> d.popleft() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Expanding World of Python

The Expanding World of Python

Dane Hillard

Publisher Resources

ISBN: 9781788294874Supplemental Content