# Queues And Stacks

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

## Preliminaries

```from collections import deque
```

## Make A Queue

```# Option 1: Make a queue
queue = deque(range(10))

# Option 2: Make a queue that, if full, discards any item at the
# opposite end to where you added an item.
queue = deque(range(10), maxlen=10)
```

## Manipulate Queue

```# Append an item to the right

queue.append('A')

# View queue
queue
```
```deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 'A'])
```
```# Append an item to the left

queue.appendleft('A')

# View queue
queue
```
```deque(['A', 1, 2, 3, 4, 5, 6, 7, 8, 9])
```
```# Count occurances of item
queue.count('A')

# View queue
queue
```
```deque(['A', 1, 2, 3, 4, 5, 6, 7, 8, 9])
```
```# Remove and return right-most item
queue.pop()

# View queue
queue
```
```deque(['A', 1, 2, 3, 4, 5, 6, 7, 8])
```
```# Remove and return left-most item
queue.popleft()

# View queue
queue
```
```deque([1, 2, 3, 4, 5, 6, 7, 8])
```
```# Insert item to the right of an item
queue.insert(2, 'A')

# View queue
queue
```
```deque([1, 2, 'A', 3, 4, 5, 6, 7, 8])
```
```# Reverse the queue
queue.reverse()

# View queue
queue
```
```deque([8, 7, 6, 5, 4, 3, 'A', 2, 1])
```
```# Delete all items
queue.clear()

# View queue
queue
```
```deque([])
```