From 6462883e1019217c094b7073b2e346c6218c514d Mon Sep 17 00:00:00 2001 From: nigelgold Date: Mon, 27 Oct 2025 17:20:56 -0400 Subject: [PATCH 1/3] please see the new term entry for the deque clear method in python. --- .../concepts/deque/terms/clear/clear.md | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 content/python/concepts/deque/terms/clear/clear.md diff --git a/content/python/concepts/deque/terms/clear/clear.md b/content/python/concepts/deque/terms/clear/clear.md new file mode 100644 index 00000000000..50cf4ca844e --- /dev/null +++ b/content/python/concepts/deque/terms/clear/clear.md @@ -0,0 +1,79 @@ +--- +Title: 'Clear' +Description: 'This function implements the clear option to remove all items from a queue' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Algorithms' + - 'Collections' + - 'Data Structures' + - 'Deques' + - 'clear' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +**Clear()** is a built-in method for arrays that allows all elements in the array to be removed. +In the instance of double ended queues this method removes every element from the deque. + +'Clear' is useful in cases where there is a quick need to removed the entire contents of queues to save spave without actually deleting the queue itself. With this method there is a linear time complexity of O(n). + +## Syntax + +```pseudo +deque.clear() +``` + +**Parameters:** + +- `None`: This method does not take any parameters as it simply resets the deque to empty + +**Return value** + +The return value of clear() should be a check to determine if the deque is empty. This can be done using an 'if' statement with a return value of 'true' when checked if empty. Or simply a print statement to output the length of the deque. + +## Example 1: clearing elements from a deque using an if statement + +This example demonstates using the clear() method with an if statement + +```py +from collections import deque + +#create a deque + +cars = deque(["toyota", "suzuki", "honda"]) +print(f"Initial deque is as follows: {cars}") + +#clearing the deque +cars.clear() + +#if statement to check if the deque has been sucessfully cleared +if not cars: + print("the deque is empty") +return +``` + + +## Codebyte Example: How to use the clear() method in practice for deques + + This example demonstrates using the clear() method in practice + + + + ```codebyte/phyton + from collections import deque + +#create a deque + +cars = deque(["toyota", "suzuki", "honda"]) +print(f"Initial deque is as follows: {cars}") + +#clearing the deque +cars.clear() + +#output check to see if the deque is empty +print(len(cars)) +``` + From 36d16558ec19247e2b8bfb87249b0c15f235b74b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 28 Oct 2025 12:09:46 +0530 Subject: [PATCH 2/3] Revise clear() method documentation for clarity Updated the documentation for the clear() method in Python's deque class to improve clarity and correctness. Adjusted examples and descriptions to reflect accurate usage and behavior. --- .../concepts/deque/terms/clear/clear.md | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/content/python/concepts/deque/terms/clear/clear.md b/content/python/concepts/deque/terms/clear/clear.md index 50cf4ca844e..9e92816cf8e 100644 --- a/content/python/concepts/deque/terms/clear/clear.md +++ b/content/python/concepts/deque/terms/clear/clear.md @@ -1,6 +1,6 @@ --- -Title: 'Clear' -Description: 'This function implements the clear option to remove all items from a queue' +Title: 'clear()' +Description: 'Removes all elements from the deque, leaving it empty.' Subjects: - 'Computer Science' - 'Data Science' @@ -9,16 +9,14 @@ Tags: - 'Collections' - 'Data Structures' - 'Deques' - - 'clear' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -**Clear()** is a built-in method for arrays that allows all elements in the array to be removed. -In the instance of double ended queues this method removes every element from the deque. +The **`clear()`** method in Python’s `collections.deque` class removes all elements from the deque, leaving it empty. It’s useful when the contents of the deque need to be cleared without deleting the object itself. -'Clear' is useful in cases where there is a quick need to removed the entire contents of queues to save spave without actually deleting the queue itself. With this method there is a linear time complexity of O(n). +The method modifies the deque in place and returns `None`. Its time complexity is O(n) since it removes elements one by one. ## Syntax @@ -30,50 +28,49 @@ deque.clear() - `None`: This method does not take any parameters as it simply resets the deque to empty -**Return value** +**Return value:** -The return value of clear() should be a check to determine if the deque is empty. This can be done using an 'if' statement with a return value of 'true' when checked if empty. Or simply a print statement to output the length of the deque. +The return value of `clear()` should be a check to determine if the deque is empty. This can be done using an 'if' statement with a return value of 'true' when checked if empty. Or simply a print statement to output the length of the deque. -## Example 1: clearing elements from a deque using an if statement +## Example 1: Clearing all elements from a deque -This example demonstates using the clear() method with an if statement +In this example, the `clear()` method removes every element from the deque, and an `if` statement checks whether it is empty afterward: ```py from collections import deque -#create a deque - +# Create a deque cars = deque(["toyota", "suzuki", "honda"]) print(f"Initial deque is as follows: {cars}") -#clearing the deque +# Clearing the deque cars.clear() -#if statement to check if the deque has been sucessfully cleared +# If statement to check if the deque has been sucessfully cleared if not cars: print("the deque is empty") -return ``` +The output of this code is: -## Codebyte Example: How to use the clear() method in practice for deques - - This example demonstrates using the clear() method in practice - +```shell +Initial deque: deque(['Toyota', 'Suzuki', 'Honda']) +The deque is empty. +``` +## Codebyte Example: Clearing completed tasks - ```codebyte/phyton - from collections import deque +In this example, a deque keeps track of recent tasks in a to-do list. After all tasks are completed, the `clear()` method is used to empty the deque and prepare it for new tasks: -#create a deque +```codebyte/python +from collections import deque -cars = deque(["toyota", "suzuki", "honda"]) -print(f"Initial deque is as follows: {cars}") +# Create a deque of completed tasks +completed_tasks = deque(["Write report", "Send emails", "Backup files"]) +print(f"Completed tasks: {completed_tasks}") -#clearing the deque -cars.clear() +# Clear all tasks to reuse the deque for a new session +completed_tasks.clear() -#output check to see if the deque is empty -print(len(cars)) +print(f"Tasks after clearing: {completed_tasks}") ``` - From 0a4e0701f9e838059ab4dc880e164856194df979 Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Fri, 31 Oct 2025 11:40:07 +0530 Subject: [PATCH 3/3] Minor changes --- .../python/concepts/deque/terms/clear/clear.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/python/concepts/deque/terms/clear/clear.md b/content/python/concepts/deque/terms/clear/clear.md index 9e92816cf8e..e167b9e56de 100644 --- a/content/python/concepts/deque/terms/clear/clear.md +++ b/content/python/concepts/deque/terms/clear/clear.md @@ -1,5 +1,5 @@ --- -Title: 'clear()' +Title: '.clear()' Description: 'Removes all elements from the deque, leaving it empty.' Subjects: - 'Computer Science' @@ -14,9 +14,9 @@ CatalogContent: - 'paths/computer-science' --- -The **`clear()`** method in Python’s `collections.deque` class removes all elements from the deque, leaving it empty. It’s useful when the contents of the deque need to be cleared without deleting the object itself. +The **`.clear()`** method in Python’s `collections.deque` class removes all elements from the deque, leaving it empty. It’s useful when the contents of the deque need to be cleared without deleting the object itself. -The method modifies the deque in place and returns `None`. Its time complexity is O(n) since it removes elements one by one. +The method modifies the deque in place and returns `None`. Its time complexity is `O(n)` since it removes elements one by one. ## Syntax @@ -26,15 +26,15 @@ deque.clear() **Parameters:** -- `None`: This method does not take any parameters as it simply resets the deque to empty +- `None`: This method does not take any parameters as it simply resets the deque to empty. **Return value:** -The return value of `clear()` should be a check to determine if the deque is empty. This can be done using an 'if' statement with a return value of 'true' when checked if empty. Or simply a print statement to output the length of the deque. +The return value of `.clear()` should be a check to determine if the deque is empty. This can be done using an `if` statement with a return value of `true` when checked if empty or simply a `print()` statement to output the length of the deque. -## Example 1: Clearing all elements from a deque +## Example 1: Clearing All Elements from a Deque -In this example, the `clear()` method removes every element from the deque, and an `if` statement checks whether it is empty afterward: +In this example, the `.clear()` method removes every element from the deque, and an `if` statement checks whether it is empty afterward: ```py from collections import deque @@ -58,9 +58,9 @@ Initial deque: deque(['Toyota', 'Suzuki', 'Honda']) The deque is empty. ``` -## Codebyte Example: Clearing completed tasks +## Codebyte Example: Clearing Completed Tasks -In this example, a deque keeps track of recent tasks in a to-do list. After all tasks are completed, the `clear()` method is used to empty the deque and prepare it for new tasks: +In this example, a deque keeps track of recent tasks in a to-do list. After all tasks are completed, the `.clear()` method is used to empty the deque and prepare it for new tasks: ```codebyte/python from collections import deque