From 91a519728765172c71518c313d1128fdbd1c900a Mon Sep 17 00:00:00 2001 From: Pessoa Date: Tue, 21 Oct 2025 13:26:14 -0300 Subject: [PATCH 1/9] [Term Entry] Python deque: index() (Codecademy#7755) * Adding index.md --------- --- .../concepts/deque/terms/index/index.md | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 content/python/concepts/deque/terms/index/index.md diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md new file mode 100644 index 00000000000..0d7a35bd036 --- /dev/null +++ b/content/python/concepts/deque/terms/index/index.md @@ -0,0 +1,96 @@ +--- +Title: 'index' +Description: 'Used to find the position of the first occurrence of an element in a deque.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Algorithms' + - 'Collections' + - 'Data Structures' + - 'Deques' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **index(x)** method is used to find the position of the first occurrence of `x` in a [deque](https://www.codecademy.com/resources/docs/python/collections-module/deque). + + +## Syntax + +```pseudo +deque.index(x[, start[, stop]]) +``` + +**Parameter:** + +- `x`: The element to be found within the deque. + +- `start`: The zero based inclusive index to specify the start position of the search. + +- `stop`: The zero based exclusive index to specify the stop position of the search. + +**Return value:** + +This method returns the zero based index of element `x` in the list. + +> **Note:** This method raises a ValueError if `x` is not in the list which can potentially break your code's execution if not treated inside a [try and except](https://www.codecademy.com/resources/docs/python/keywords/try) block. + + +## Example + +In this example, `index()` is used to find: +- The very first position of an element in a list. +- The position of an element in a list after a given postion. +- The position of an element in a list inside a given window. +- The position of an element that is not in the list. + +```py +from collections import deque + +# Create a deque +dq = deque([1, 2, 3, 1, 4, 5, 1]) + +# Find the very first position of (4) +print("Very first position of (4):", dq.index(4)) + +# Find the position of (1) after index=0. +start = 0 + 1 # Since the start parameter is inclusive, we add 1 to exclude index=0. +print("Position of (1) after index=0:", dq.index(1,1)) + +# Find the position of (1) in a given window. +start = 1 +stop = 6 +print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.index(1,1,6)) + +# Find the position of (10) which is not in the list. +try: + print("Position of (10):", dq.index(10)) +except(ValueError) as ve: + print("Trying to find the index of a nonexistent element will give you problems.") + print(ve) +# This will raise an error +# ValueError: 10 is not in deque +``` + +## Codebyte Example: Finding the index of elements in a list + +In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing it's possible applications and flexibility. + +```codebyte/python +from collections import deque + +# Create a deque with some initial elements +fruits = deque(["apple", "banana", "apple", "lemon", "apple","tomato"]) + +# Find the index for the first occurrence of "apple" +first_occurrence_of_apple_index = fruits.index("apple") +print("First found index for apple: ", first_occurrence_of_apple_index) + +# Find the next occurrence of apple after the first occurrence +print("Next occurrence: ", fruits.index("apple",first_occurrence_of_apple_index + 1)) + +# Find the index for apple between index=1 inclusive and index=4 exclusive. +print("Index for apple, between (1) inclusive and (4) exclusive: ", fruits.index("apple",1,4)) +``` \ No newline at end of file From 82b24d48c6a4de0e9aba57ada8894755d89bf3cb Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 24 Oct 2025 16:31:42 +0530 Subject: [PATCH 2/9] minor content fixes --- .../concepts/deque/terms/index/index.md | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index 0d7a35bd036..ade5c5dc1a7 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -1,6 +1,6 @@ --- Title: 'index' -Description: 'Used to find the position of the first occurrence of an element in a deque.' +Description: 'Returns the position of the first occurrence of a specified element within the deque.' Subjects: - 'Computer Science' - 'Data Science' @@ -14,37 +14,29 @@ CatalogContent: - 'paths/computer-science' --- -The **index(x)** method is used to find the position of the first occurrence of `x` in a [deque](https://www.codecademy.com/resources/docs/python/collections-module/deque). - +The **`index()`** method in a Python [deque](https://www.codecademy.com/resources/docs/python/collections-module/deque) returns the position of the first occurrence of a specified element within the deque. ## Syntax ```pseudo -deque.index(x[, start[, stop]]) +deque.index(element, start=0, stop=len(deque)) ``` -**Parameter:** - -- `x`: The element to be found within the deque. - -- `start`: The zero based inclusive index to specify the start position of the search. +**Parameters:** -- `stop`: The zero based exclusive index to specify the stop position of the search. +- `element`: The element to be found within the deque. +- `start` (Optional): The zero based inclusive index to specify the start position of the search. +- `stop` (Optional): The index to end the search (default is the length of the deque). **Return value:** -This method returns the zero based index of element `x` in the list. +Returns the index (int) of the first matching element; raises a `ValueError` if the element is not found. -> **Note:** This method raises a ValueError if `x` is not in the list which can potentially break your code's execution if not treated inside a [try and except](https://www.codecademy.com/resources/docs/python/keywords/try) block. +> **Note:** This method raises a `ValueError` if the element is not in the deque, which can stop the code if not handled inside a [try and except](https://www.codecademy.com/resources/docs/python/keywords/try) block. +## Example 1: Finding elements by position -## Example - -In this example, `index()` is used to find: -- The very first position of an element in a list. -- The position of an element in a list after a given postion. -- The position of an element in a list inside a given window. -- The position of an element that is not in the list. +In this example, `index()` is used to find the position of elements in different scenarios: ```py from collections import deque @@ -66,19 +58,27 @@ print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.ind # Find the position of (10) which is not in the list. try: - print("Position of (10):", dq.index(10)) + print("Position of (10):", dq.index(10)) except(ValueError) as ve: - print("Trying to find the index of a nonexistent element will give you problems.") - print(ve) -# This will raise an error -# ValueError: 10 is not in deque + print("Trying to find the index of a nonexistent element will give you problems.") + print(ve) +``` + +The output of this code is: + +```shell +Very first position of (4): 4 +Position of (1) after index=0: 3 +Position of (1) between index=1 inclusive and index=6 exclusive: 3 +Trying to find the index of a nonexistent element will give you problems. +10 is not in deque ``` -## Codebyte Example: Finding the index of elements in a list +## Example 2: Finding elements by value range -In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing it's possible applications and flexibility. +In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing its flexibility and use of parameters: -```codebyte/python +```py from collections import deque # Create a deque with some initial elements @@ -93,4 +93,12 @@ print("Next occurrence: ", fruits.index("apple",first_occurrence_of_apple_index # Find the index for apple between index=1 inclusive and index=4 exclusive. print("Index for apple, between (1) inclusive and (4) exclusive: ", fruits.index("apple",1,4)) -``` \ No newline at end of file +``` + +The output of this code is: + +```shell +First found index for apple: 0 +Next occurrence: 2 +Index for apple, between (1) inclusive and (4) exclusive: 2 +``` From 30cafb800c07d5779df4efd81d527a53e6814b67 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 24 Oct 2025 16:33:20 +0530 Subject: [PATCH 3/9] fixed typo --- .../concepts/deque/terms/index/index.md | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index ade5c5dc1a7..a5ea094ae2f 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -47,20 +47,20 @@ dq = deque([1, 2, 3, 1, 4, 5, 1]) # Find the very first position of (4) print("Very first position of (4):", dq.index(4)) -# Find the position of (1) after index=0. -start = 0 + 1 # Since the start parameter is inclusive, we add 1 to exclude index=0. -print("Position of (1) after index=0:", dq.index(1,1)) +# Find the position of (1) after index=0 +start = 0 + 1 # Since the start parameter is inclusive, add 1 to skip index=0. +print("Position of (1) after index=0:", dq.index(1, 1)) -# Find the position of (1) in a given window. +# Find the position of (1) in a given window start = 1 stop = 6 -print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.index(1,1,6)) +print("Position of (1) between index=1 inclusive and index=6 exclusive:", dq.index(1, 1, 6)) -# Find the position of (10) which is not in the list. +# Find the position of (10), which is not in the deque try: print("Position of (10):", dq.index(10)) -except(ValueError) as ve: - print("Trying to find the index of a nonexistent element will give you problems.") +except ValueError as ve: + print("Trying to find the index of a nonexistent element will cause an error.") print(ve) ``` @@ -82,17 +82,17 @@ In this example, `index()` finds the position of the element `"apple"` in differ from collections import deque # Create a deque with some initial elements -fruits = deque(["apple", "banana", "apple", "lemon", "apple","tomato"]) +fruits = deque(["apple", "banana", "apple", "lemon", "apple", "tomato"]) # Find the index for the first occurrence of "apple" first_occurrence_of_apple_index = fruits.index("apple") -print("First found index for apple: ", first_occurrence_of_apple_index) +print("First found index for apple:", first_occurrence_of_apple_index) -# Find the next occurrence of apple after the first occurrence -print("Next occurrence: ", fruits.index("apple",first_occurrence_of_apple_index + 1)) +# Find the next occurrence of "apple" after the first occurrence +print("Next occurrence:", fruits.index("apple", first_occurrence_of_apple_index + 1)) -# Find the index for apple between index=1 inclusive and index=4 exclusive. -print("Index for apple, between (1) inclusive and (4) exclusive: ", fruits.index("apple",1,4)) +# Find the index for "apple" between index=1 inclusive and index=4 exclusive +print("Index for apple between (1) inclusive and (4) exclusive:", fruits.index("apple", 1, 4)) ``` The output of this code is: From ed3b682636009fd044dd2400c1bbdd688490a32f Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:50:27 +0530 Subject: [PATCH 4/9] Apply suggestion from @avdhoottt --- content/python/concepts/deque/terms/index/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index a5ea094ae2f..b74943707ed 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -25,12 +25,12 @@ deque.index(element, start=0, stop=len(deque)) **Parameters:** - `element`: The element to be found within the deque. -- `start` (Optional): The zero based inclusive index to specify the start position of the search. +- `start` (Optional): The zero-based inclusive index to specify the start position of the search. - `stop` (Optional): The index to end the search (default is the length of the deque). **Return value:** -Returns the index (int) of the first matching element; raises a `ValueError` if the element is not found. +Returns the index (`int`) of the first matching element; raises a `ValueError` if the element is not found. > **Note:** This method raises a `ValueError` if the element is not in the deque, which can stop the code if not handled inside a [try and except](https://www.codecademy.com/resources/docs/python/keywords/try) block. From dbc87c2716eca1bb210f7c9ea438560539b13f41 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:51:29 +0530 Subject: [PATCH 5/9] Apply suggestion from @avdhoottt --- content/python/concepts/deque/terms/index/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index b74943707ed..2d55cff1c40 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -74,7 +74,7 @@ Trying to find the index of a nonexistent element will give you problems. 10 is not in deque ``` -## Example 2: Finding elements by value range +## Codebyte Example: Finding elements by value range In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing its flexibility and use of parameters: From a95c4c7baad76217215dfe23b5c37ea8f5716c1a Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:52:08 +0530 Subject: [PATCH 6/9] Apply suggestion from @avdhoottt --- content/python/concepts/deque/terms/index/index.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index 2d55cff1c40..94e019492b0 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -95,10 +95,3 @@ print("Next occurrence:", fruits.index("apple", first_occurrence_of_apple_index print("Index for apple between (1) inclusive and (4) exclusive:", fruits.index("apple", 1, 4)) ``` -The output of this code is: - -```shell -First found index for apple: 0 -Next occurrence: 2 -Index for apple, between (1) inclusive and (4) exclusive: 2 -``` From 2ba8f859ae43eba6fc0af71edae75e8bca23b23e Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:53:19 +0530 Subject: [PATCH 7/9] Apply suggestion from @avdhoottt --- content/python/concepts/deque/terms/index/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index 94e019492b0..3a1c3806a8f 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -78,7 +78,7 @@ Trying to find the index of a nonexistent element will give you problems. In this example, `index()` finds the position of the element `"apple"` in different scenarios, showing its flexibility and use of parameters: -```py +```codebyte/python from collections import deque # Create a deque with some initial elements From bf1e00d7c69b0467a29eb32e6108828506329f29 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:54:08 +0530 Subject: [PATCH 8/9] Apply suggestion from @avdhoottt --- content/python/concepts/deque/terms/index/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index 3a1c3806a8f..fc5e53f2cd6 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -70,7 +70,7 @@ The output of this code is: Very first position of (4): 4 Position of (1) after index=0: 3 Position of (1) between index=1 inclusive and index=6 exclusive: 3 -Trying to find the index of a nonexistent element will give you problems. +Trying to find the index of a nonexistent element will cause an error. 10 is not in deque ``` From 0efee99573c4790c75e8ff70574384c3834d7400 Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Thu, 30 Oct 2025 20:02:52 +0530 Subject: [PATCH 9/9] Lint --- content/python/concepts/deque/terms/index/index.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/python/concepts/deque/terms/index/index.md b/content/python/concepts/deque/terms/index/index.md index fc5e53f2cd6..cfd7a39a45c 100644 --- a/content/python/concepts/deque/terms/index/index.md +++ b/content/python/concepts/deque/terms/index/index.md @@ -11,7 +11,7 @@ Tags: - 'Deques' CatalogContent: - 'learn-python-3' - - 'paths/computer-science' + - 'paths/computer-science' --- The **`index()`** method in a Python [deque](https://www.codecademy.com/resources/docs/python/collections-module/deque) returns the position of the first occurrence of a specified element within the deque. @@ -94,4 +94,3 @@ print("Next occurrence:", fruits.index("apple", first_occurrence_of_apple_index # Find the index for "apple" between index=1 inclusive and index=4 exclusive print("Index for apple between (1) inclusive and (4) exclusive:", fruits.index("apple", 1, 4)) ``` -