<jupyter_start><jupyter_text>Populating<jupyter_code>import collections
d1 = collections.deque()
d1.extend('abcdefg')
print('extend', d1)
d1.append('h')
print('append', d1)
d2 = collections.deque()
d2.extendleft(range(6))
print('extend left', d2)
d2.appendleft(6)
print('append left', d2)<jupyter_output>extend left deque([5, 4, 3, 2, 1, 0])
append left deque([6, 5, 4, 3, 2, 1, 0])<jupyter_text>Consuming<jupyter_code>import collections
print('From the right')
d = collections.deque('abcdef')
while True:
    try:
        print(d.pop(), end=' ')
    except IndexError:
        break
print()
print('\n From the left:')
d = collections.deque(range(6))
while True:
    try:
        print(d.popleft(), end=' ')
    except IndexError:
        break<jupyter_output>From the right
f e d c b a 

 From the left:
0 1 2 3 4 5<jupyter_text>the deque is thread safe<jupyter_code>import collections
import threading
import time

candle = collections.deque(range(5))
def burn(direction, nextSource):
    while True:
        try:
            next = nextSource()
        except IndexError:
            break
        else:
            print('{:>8}:{}'.format(direction, next))
            time.sleep(0.1)
    print('{:>8} done'.format(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()<jupyter_output>Left:0   Right:4

   Right:3    Left:1

    Left:2   Right done

    Left done<jupyter_text>Rotating<jupyter_code>import collections
d = collections.deque(range(10))
print('Normal    :', d)
d = collections.deque(range(10))
d.rotate(2)
print('Right rotation :', d)
d = collections.deque(range(10))
d.rotate(-2)
print('Left rotation :', d)<jupyter_output>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])<jupyter_text>Constraining the Queue Size<jupyter_code>import collections
import random
random.seed(1)
d1 = collections.deque(maxlen=3)
d2 = collections.deque(maxlen=3)

for i in range(4):
    n = random.randint(0, 100)
    print('n= ', n)
    d1.append(n)
    d2.appendleft(n)
    print('D1:' ,d1)
    print('D2:', d2)<jupyter_output>n=  17
D1: deque([17], maxlen=3)
D2: deque([17], maxlen=3)
n=  72
D1: deque([17, 72], maxlen=3)
D2: deque([72, 17], maxlen=3)
n=  97
D1: deque([17, 72, 97], maxlen=3)
D2: deque([97, 72, 17], maxlen=3)
n=  8
D1: deque([72, 97, 8], maxlen=3)
D2: deque([8, 97, 72], maxlen=3)
{'fraction_non_alphanumeric': 0.17017954722872755, 'fraction_numerical': 0.059718969555035126, 'mean_word_length': 4.115768463073852, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '335798', 'license': 'MIT', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1019, 'n_words': 249, 'path': 'Python-Standard-Library/DataStructures/collection/deque.ipynb', 'repo_name': 'gaufung/CodeBase', 'size': '2562'}