|
| 1 | +--- |
| 2 | +Title: 'insert()' |
| 3 | +Description: 'Inserts a single element at the specified index in the deque.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Collections' |
| 9 | + - 'Data Structures' |
| 10 | + - 'Deques' |
| 11 | + - 'Methods' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`insert()`** method of Python’s [`collections.deque`](https://www.codecademy.com/resources/docs/python/collections-module) inserts an element at a specified index within the deque. Unlike `append()` or `appendleft()`, which add elements only to the ends, `insert()` can add an element at any valid position. |
| 18 | + |
| 19 | +While deques are optimized for fast appends and pops at both ends, using `insert()` in the middle may affect performance because it shifts elements internally. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +deque.insert(index, element) |
| 25 | +``` |
| 26 | + |
| 27 | +**Parameters:** |
| 28 | + |
| 29 | +- `index` : The position where the new element will be inserted. |
| 30 | +- `element` : The item to insert into the deque. |
| 31 | + |
| 32 | +**Return value:** |
| 33 | + |
| 34 | +- `None`: This method modifies the deque in place. |
| 35 | + |
| 36 | +## Example 1: Inserting an Element at a Specific Position |
| 37 | + |
| 38 | +This example demonstrates inserting elements at specific positions within a deque: |
| 39 | + |
| 40 | +```py |
| 41 | +from collections import deque |
| 42 | + |
| 43 | +# Create a deque |
| 44 | +fruits = deque(["apple", "banana", "cherry"]) |
| 45 | +print("Original deque:", fruits) |
| 46 | + |
| 47 | +# Insert 'orange' at index 1 |
| 48 | +fruits.insert(1, "orange") |
| 49 | +print("After inserting 'orange' at index 1:", fruits) |
| 50 | +``` |
| 51 | + |
| 52 | +The output of this code is: |
| 53 | + |
| 54 | +```shell |
| 55 | +Original deque: deque(['apple', 'banana', 'cherry']) |
| 56 | +After inserting 'orange' at index 1: deque(['apple', 'orange', 'banana', 'cherry']) |
| 57 | +``` |
| 58 | + |
| 59 | +## Example 2: Using Negative Indices and Edge Insertions |
| 60 | + |
| 61 | +This example demonstrates inserting elements at the beginning and end using positive and negative indices: |
| 62 | + |
| 63 | +```py |
| 64 | +from collections import deque |
| 65 | + |
| 66 | +colors = deque(["red", "green", "blue"]) |
| 67 | + |
| 68 | +# Insert at the beginning |
| 69 | +colors.insert(0, "purple") |
| 70 | + |
| 71 | +# Insert at the end using len(colors) |
| 72 | +colors.insert(len(colors), "yellow") |
| 73 | + |
| 74 | +# Insert before the last item using negative index |
| 75 | +colors.insert(-1, "cyan") |
| 76 | + |
| 77 | +print(colors) |
| 78 | +``` |
| 79 | + |
| 80 | +The output of this code is: |
| 81 | + |
| 82 | +```shell |
| 83 | +deque(['purple', 'red', 'green', 'blue', 'cyan', 'yellow']) |
| 84 | +``` |
| 85 | + |
| 86 | +Here, the `insert()` method demonstrates its flexibility that it can insert items anywhere in the deque. |
| 87 | + |
| 88 | +## Codebyte Example: How to Use `deque.insert()` for Dynamic Insertions |
| 89 | + |
| 90 | +In this example, `insert()` adds elements at various positions in a deque, demonstrating dynamic insertions at the beginning, middle, and end: |
| 91 | + |
| 92 | +```codebyte/python |
| 93 | +from collections import deque |
| 94 | +
|
| 95 | +# Create a deque of numbers |
| 96 | +numbers = deque([10, 20, 30, 40]) |
| 97 | +print("Original deque:", numbers) |
| 98 | +
|
| 99 | +# Insert elements at various positions |
| 100 | +numbers.insert(2, 25) # Insert at index 2 |
| 101 | +numbers.insert(0, 5) # Insert at beginning |
| 102 | +numbers.insert(len(numbers), 50) # Insert at end |
| 103 | +
|
| 104 | +print("Updated deque:", numbers) |
| 105 | +
|
| 106 | +# Access elements to verify insertion |
| 107 | +print("First element:", numbers[0]) |
| 108 | +print("Middle element:", numbers[2]) |
| 109 | +print("Last element:", numbers[-1]) |
| 110 | +``` |
0 commit comments