# Queue (Data Structure)

> A line where the first item in is the first one out. Join at the back.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Queue, FIFO, First In, First Out

## Think of it like

Think of it like a line at a shop. The first person in line is the first one served.

## Definition

A queue is a list where items join at the back and leave from the front. The first item in is the first one out, which is called first in, first out. Adding is called enqueue and removing is called dequeue. Print jobs, messages and tasks waiting for a worker all sit in queues. In Python, a deque does both ends quickly.

## Example

Emma and then Jack send pages to the office printer. The queue prints Emma's page first, because it arrived first.

## Example code

```python
>>> from collections import deque
>>> q = deque()
>>> q.append("Emma")
>>> q.append("Jack")
>>> q.popleft()
'Emma'
```

Emma joined first, so Emma leaves first.

## Why it matters

Queues keep work fair and in order when it arrives faster than it can be done.

## Common mistake

Using a plain Python list and pop(0) as a queue. Each removal shifts every item; deque does it in one step.

## Three principles

- **First in, first out.** Like a line at a shop.
- **Back in, front out.** Enqueue and dequeue.
- **Keeps order.** Fair for waiting work.

## Related terms

- [Stack (Data Structure)](https://learn.benibix.com/term/development/stack)
- [Data Structure](https://learn.benibix.com/term/development/data-structure)
- [Linked List](https://learn.benibix.com/term/development/linked-list)
- [Breadth-First Search (BFS)](https://learn.benibix.com/term/development/breadth-first-search)

## Told apart from

- [Stack vs Queue](https://learn.benibix.com/compare/stack-vs-queue): A stack and a queue both hold items in a line, but they let them out from different ends. A stack gives back the last item in. A queue gives back the first.

---

Source: https://learn.benibix.com/term/development/queue
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
