text: - 2. It uses a binary search to find the insertion point for new items and is an alternative to repeatedly sorting a list that changes frequently.
  Although the built-in list can simulate a queue using the insert() and pop() methods, it is not thread-safe. For true ordered communication between threads, use the Queue. Counts',  words in the system dictionary to produce a frequency distribution, and '\nCombined counts:' print c1 + c2 print '\nSubtraction:' print c1 - c2 print '\nIntersection (taking positive minimums):' print c1 & c2 print '\nUnion }), stacks, and queues are degenerate forms of deques where the inputs and outputs are restricted to a single end.
  import collections d = collections.deque('abcdefg') print 'Deque:', d print 'Length:', len(d) print 'Left end:', d[0] print 'Right end:', d[-1] d.remove('c') print 'remove(c):', d
  Since deques are a type of sequence container, they support some of the same operations as list, such as examining the contents with __getitem__(), determining length, and removing elements from the middle by matching identity.
  $ python collections_deque.py Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g']) Length: 7 Left end: a Right end: g remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])
  Populating)) def burn(direction, nextSource): while True: try: next = nextSource() except IndexError: break else: print '%8s: %s' % (direction, next) time.sleep(0.1) print '%8s done' % direction return left = threading.Thread(target=burn, args=('Left', candle.popleft)) right = threading.Thread(target=burn, args=('Right', candle.pop)) left.start() right.start() left.join() right.join()
  The threads in this example alternate between each end, removing items until the deque is empty.
  $ python collections_deque_both_ends.py Left: 0 Right: 4 Right: 3 Left: 1 Right: 2 Left done Right done
  Rotating
  Another useful capability of the deque is to rotate it in either direction, to skip over some items.
  import collections d = collections.deque(xrange(10)) print 'Normal :', d d = collections.deque(xrange(10)) d.rotate(2) print 'Right rotation:', d d = collections.deque(xrange(10)) d.rotate(-2) print 'Left rotation :', d
  Rotating the deque to the right (using a positive rotation) takes items from the right end and moves them to the left end. Rotating to the left (with a negative value) takes items from the left end and moves them to the right end. It may help to visualize the items in the deque as being engraved along the edge of a dial.
  $ python collections_deque_rotate.py Normal : deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Right rotation: deque([8, 9, 0, 1, 2, 3, 4, 5, 6, 7]) Left rotation : deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
  See Also:
  -.