From 4f46b2174fdee0b231b83bd92b8d222d81d5c4d5 Mon Sep 17 00:00:00 2001 From: LasVegas98 Date: Wed, 15 Oct 2025 12:38:41 +0100 Subject: [PATCH 1/4] add popleft() entry --- .../concepts/deque/terms/popleft/popleft.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 content/python/concepts/deque/terms/popleft/popleft.md diff --git a/content/python/concepts/deque/terms/popleft/popleft.md b/content/python/concepts/deque/terms/popleft/popleft.md new file mode 100644 index 00000000000..e1a40eb6eaf --- /dev/null +++ b/content/python/concepts/deque/terms/popleft/popleft.md @@ -0,0 +1,49 @@ +--- +Title: 'popleft()' +Description: 'Removes and returns an element from the left end of a deque object in Python.' +Subjects: + - 'Python' + - 'Data Structures' +Tags: + - 'Deque' + - 'Collections' + - 'Methods' + - 'Queues' +CatalogContent: + - 'learn-python-3' + - 'python-data-structures' +--- + +## Definition + +The **`popleft()`** method removes and returns the element from the **left side** (the first element) of a `deque` object. +A `deque` (double-ended queue) allows elements to be added or removed efficiently from both ends. + +If the deque is empty, calling `popleft()` raises an **`IndexError`**. + +--- + +## Syntax + +```python +deque.popleft() + +from collections import deque + +# Create a deque with several elements +queue = deque(['apple', 'banana', 'cherry']) + +# Remove the leftmost element +left_item = queue.popleft() + +print(left_item) # Output: apple +print(queue) # Output: deque(['banana', 'cherry']) + +from collections import deque + +# Demonstration of popleft() +fruits = deque(['apple', 'banana', 'cherry']) +removed_item = fruits.popleft() + +print("Removed:", removed_item) +print("Remaining deque:", fruits) From 264a768e5cac777746b302b2aa74a13636a85ee7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 16 Oct 2025 16:08:38 +0530 Subject: [PATCH 2/4] minor updates --- .../concepts/deque/terms/popleft/popleft.md | 60 +++++++++++++------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/content/python/concepts/deque/terms/popleft/popleft.md b/content/python/concepts/deque/terms/popleft/popleft.md index e1a40eb6eaf..a0e8091a065 100644 --- a/content/python/concepts/deque/terms/popleft/popleft.md +++ b/content/python/concepts/deque/terms/popleft/popleft.md @@ -1,12 +1,12 @@ --- -Title: 'popleft()' +Title: '.popleft()' Description: 'Removes and returns an element from the left end of a deque object in Python.' Subjects: - - 'Python' + - 'Code Foundations' - 'Data Structures' Tags: - - 'Deque' - 'Collections' + - 'Deques' - 'Methods' - 'Queues' CatalogContent: @@ -14,20 +14,27 @@ CatalogContent: - 'python-data-structures' --- -## Definition +The **`deque.popleft()`** method removes and returns the element from the left end (first element) of a `deque` object. A `deque` (double-ended queue) allows elements to be added or removed efficiently from both ends. Calling `popleft()` on an empty deque raises an `IndexError`. -The **`popleft()`** method removes and returns the element from the **left side** (the first element) of a `deque` object. -A `deque` (double-ended queue) allows elements to be added or removed efficiently from both ends. +## Syntax -If the deque is empty, calling `popleft()` raises an **`IndexError`**. +```pseudo +deque.popleft() +``` ---- +**Parameters:** -## Syntax +The `.popleft()` method does not take any parameters. -```python -deque.popleft() +**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 @@ -35,15 +42,32 @@ 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']) +``` -print(left_item) # Output: apple -print(queue) # Output: 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 -# Demonstration of popleft() -fruits = deque(['apple', 'banana', 'cherry']) -removed_item = fruits.popleft() +# 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("Removed:", removed_item) -print("Remaining deque:", fruits) +print("Queue after serving:", list(customers)) +``` From b46cc79361ada94af3da1e12972ac2654d44c280 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 16 Oct 2025 16:09:10 +0530 Subject: [PATCH 3/4] Fix formatting of deque.popleft() description --- content/python/concepts/deque/terms/popleft/popleft.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/deque/terms/popleft/popleft.md b/content/python/concepts/deque/terms/popleft/popleft.md index a0e8091a065..bfbca667f2b 100644 --- a/content/python/concepts/deque/terms/popleft/popleft.md +++ b/content/python/concepts/deque/terms/popleft/popleft.md @@ -14,7 +14,7 @@ CatalogContent: - 'python-data-structures' --- -The **`deque.popleft()`** method removes and returns the element from the left end (first element) of a `deque` object. A `deque` (double-ended queue) allows elements to be added or removed efficiently from both ends. Calling `popleft()` on an empty deque raises an `IndexError`. +The **`deque.popleft()`** method removes and returns the element from the left end (first element) of a `deque` object. A `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 From 8cecc4f2099a1c7b597f76c910de725a75dabfeb Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 22 Oct 2025 19:03:12 +0530 Subject: [PATCH 4/4] Update popleft.md --- content/python/concepts/deque/terms/popleft/popleft.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/deque/terms/popleft/popleft.md b/content/python/concepts/deque/terms/popleft/popleft.md index bfbca667f2b..2d13060c96b 100644 --- a/content/python/concepts/deque/terms/popleft/popleft.md +++ b/content/python/concepts/deque/terms/popleft/popleft.md @@ -14,7 +14,7 @@ CatalogContent: - 'python-data-structures' --- -The **`deque.popleft()`** method removes and returns the element from the left end (first element) of a `deque` object. A `deque` (double-ended queue) allows elements to be added or removed efficiently from both ends. Calling `.popleft()` on an empty deque raises an `IndexError`. +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