From 601f856183847399edc5c748121f4fe37ae1f7c2 Mon Sep 17 00:00:00 2001 From: sahil afrid farookhi Date: Wed, 15 Oct 2025 17:25:45 -0500 Subject: [PATCH 1/2] add appendleft term --- .../deque/terms/appendleft/appendleft.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 content/python/concepts/deque/terms/appendleft/appendleft.md diff --git a/content/python/concepts/deque/terms/appendleft/appendleft.md b/content/python/concepts/deque/terms/appendleft/appendleft.md new file mode 100644 index 00000000000..b8a08815fb9 --- /dev/null +++ b/content/python/concepts/deque/terms/appendleft/appendleft.md @@ -0,0 +1,50 @@ +--- +Title: 'appendleft' +Description: 'Adds an element to the left side (beginning) of a `collections.deque` object.' +Subjects: + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Deques' + - 'Methods' + - 'Python' +CatalogContent: + - 'learn-python-3' +--- + +The **`appendleft()`** method adds an element to the left side (the beginning) of a `collections.deque` object. This is useful for managing elements in a queue-like structure, where items are added and removed from opposite ends. + +## Syntax + +```py +from collections import deque + +my_deque.appendleft(element) # element: The item to add to the left side of the deque. +``` + +## Example 1: How to add elements to the left side of a deque using appendleft() + +```py +from collections import deque + +# Create a deque +my_deque = deque([1, 2, 3]) +print(f"Initial deque: {my_deque}") + +# Append an element to the left +my_deque.appendleft(0) +print(f"Deque after appendleft(0): {my_deque}") + +# Append another element +my_deque.appendleft(-1) +print(f"Deque after appendleft(-1): {my_deque}") +``` + +The output of the example 1: + +```shell +Initial deque: deque([1, 2, 3]) +Deque after appendleft(0): deque([0, 1, 2, 3]) +Deque after appendleft(-1): deque([-1, 0, 1, 2, 3]) +``` From 7c854a5da54c60f3c68b97c355d348226d57fb74 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 17 Oct 2025 16:20:29 +0530 Subject: [PATCH 2/2] added codebyte example --- .../deque/terms/appendleft/appendleft.md | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/content/python/concepts/deque/terms/appendleft/appendleft.md b/content/python/concepts/deque/terms/appendleft/appendleft.md index b8a08815fb9..465c26fee83 100644 --- a/content/python/concepts/deque/terms/appendleft/appendleft.md +++ b/content/python/concepts/deque/terms/appendleft/appendleft.md @@ -1,8 +1,9 @@ --- -Title: 'appendleft' -Description: 'Adds an element to the left side (beginning) of a `collections.deque` object.' +Title: 'appendleft()' +Description: 'Adds an element to the left end (front) of the deque.' Subjects: - 'Computer Science' + - 'Data Science' Tags: - 'Collections' - 'Data Structures' @@ -11,19 +12,28 @@ Tags: - 'Python' CatalogContent: - 'learn-python-3' + - 'paths/computer-science' --- -The **`appendleft()`** method adds an element to the left side (the beginning) of a `collections.deque` object. This is useful for managing elements in a queue-like structure, where items are added and removed from opposite ends. +The **`appendleft()`** method adds an element to the left end (front) of a `collections.deque` object. This is useful for managing elements in a queue-like structure, where items are added and removed from opposite ends. ## Syntax -```py -from collections import deque - -my_deque.appendleft(element) # element: The item to add to the left side of the deque. +```pseudo +deque.appendleft(element) ``` -## Example 1: How to add elements to the left side of a deque using appendleft() +**Parameters:** + +- `element`: The item to add to the left end (front) of the deque. + +**Return value:** + +- `None`. The deque is modified in place. + +## Example: Adding elements to the left side of a deque using `appendleft()` + +In this example, `appendleft()` adds elements to the left end of a deque, demonstrating how items are prepended to the front: ```py from collections import deque @@ -41,10 +51,33 @@ my_deque.appendleft(-1) print(f"Deque after appendleft(-1): {my_deque}") ``` -The output of the example 1: +The output of this code is: ```shell Initial deque: deque([1, 2, 3]) Deque after appendleft(0): deque([0, 1, 2, 3]) Deque after appendleft(-1): deque([-1, 0, 1, 2, 3]) ``` + +## Codebyte Example: Using `appendleft()` for Dynamic Queue Operations + +In this example, `appendleft()` is used to add elements to the front of a deque, simulating dynamic queue operations where new items arrive at the front: + +```codebyte/python +from collections import deque + +# Initialize a deque with numbers +numbers = deque([10, 20, 30]) +print("Original deque:", numbers) + +# Add elements dynamically to the left +numbers.appendleft(5) +numbers.appendleft(0) + +print("Deque after dynamic prepend operations:", numbers) + +# Access elements to verify positions +print("First element:", numbers[0]) +print("Second element:", numbers[1]) +print("Last element:", numbers[-1]) +```