Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions content/python/concepts/deque/terms/popleft/popleft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
Title: '.popleft()'
Description: 'Removes and returns an element from the left end of a deque object in Python.'
Subjects:
- 'Code Foundations'
- 'Data Structures'
Tags:
- 'Collections'
- 'Deques'
- 'Methods'
- 'Queues'
CatalogContent:
- 'learn-python-3'
- 'python-data-structures'
---

The **`deque.popleft()`** method removes and returns the element from the left end (first element) of a `deque` object. A [`deque`](https://www.codecademy.com/resources/docs/python/collections-module/deque) (double-ended queue) allows elements to be added or removed efficiently from both ends. Calling `.popleft()` on an empty deque raises an `IndexError`.

## Syntax

```pseudo
deque.popleft()
```

**Parameters:**

The `.popleft()` method does not take any parameters.

**Return value:**

Returns the element removed from the left end of the deque.

## Example

In this example, the first element is removed from the left end of a deque:

```py
from collections import deque

# Create a deque with several elements
queue = deque(['apple', 'banana', 'cherry'])

# Remove the leftmost element
left_item = queue.popleft()
print("Removed element:", left_item)
print("Deque after popleft():", queue)
```

The output of this code is:

```shell
Removed element: apple
Deque after popleft(): deque(['banana', 'cherry'])
```

## Codebyte Example

In this example, a deque is used to simulate a queue of customers. The first customer is served using `.popleft()`:

```codebyte/python
from collections import deque

# Queue of customers
customers = deque(["Alice", "Bob", "Charlie", "Diana"])

print("Queue before serving:", list(customers))

# Serve the first customer
served_customer = customers.popleft()
print("Serving customer:", served_customer)

print("Queue after serving:", list(customers))
```