Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions content/python/concepts/deque/terms/extend/extend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
Title: 'extend()'
Description: 'Adds multiple elements to the right end of a deque from any iterable.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Algorithms'
- 'Collections'
- 'Data Structures'
- 'Deques'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`extend()`** method adds multiple elements to the right end of a `deque` from any iterable (like a [list](https://www.codecademy.com/resources/docs/python/lists), [tuple](https://www.codecademy.com/resources/docs/python/tuples), or another deque). It modifies the deque in place and returns `None`.

## Syntax

```pseudo
deque.extend(iterable)
```

**Parameters:**

- `iterable`: A sequence or iterable whose elements will be added to the right end of the deque.

**Return value:**

This method does not return any value. It modifies the deque in-place.

## Example

In this example, `extend()` adds elements from a list and another deque to the right end of a deque:

```py
from collections import deque

# Create a deque
dq = deque([1, 2, 3])

# Extend it
dq.extend([4, 5, 6])
print("Extending with list: ", dq)

#Extend it using another deque
dq.extend(deque([7, 8, 9]))
print("Extending with another deque: ", dq)
```

This example results in the following output:

```shell
Extending with list: deque([1, 2, 3, 4, 5, 6])
Extending with another deque: deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
```

## Codebyte Example: Extending a deque with a list and a tuple

In this example, `extend()` adds elements from a list and a tuple, demonstrating its ability to handle different iterables:

```codebyte/python
from collections import deque

# Create a deque with some initial elements
fruits = deque(["apple","banana"])

print("Initital deque:", fruits)

# Add multiple elements to the right using extend()
fruits.extend(["cherry", "date", "elderberry"])

# Extend again using a tuple
fruits.extend(("fig", "grape"))
print("Deque after extending with a tuple:", fruits)
```
87 changes: 87 additions & 0 deletions content/python/concepts/deque/terms/pop/pop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
Title: 'pop' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed
Description: 'Removes and returns element from the right end of the queue' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews)
Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR!
- 'Computer Science'
- 'Data Science'
Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR!
- 'Algorithms'
- 'Collections'
- 'Data Structures'
- 'Deques'
CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first
- 'learn-python-3'
- 'paths/computer-science'
---

**deque.pop()** Removes and returns an element from the right end of the deque.
If the deque is empty, calling ```pop()``` raises an ```IndexError```
The ```pop()``` method works in O(1) time complexity - removing an element from either end of a deque is constant time.


## Syntax

```pseudo
deque.pop()
```

**Parameters**

This method does not take any parameters

**Return Value**

Returns the element that was removed from the right end of the deque

**Exceptions**

- ```IndexError``` - Raised if the deque is empty when ```pop()``` is called

## Example

This example demonstrates basic deque operation ```pop()```

```py
from collections import deque

# Create a deque
dq = deque([10, 20, 30, 40])

# Remove and return the rightmost element
item = dq.pop()
print("Removed element:", item)
print("Deque after pop:", dq)
```

This example results in the following output

```shell
Removed element: 40
Deque after pop: deque([10, 20, 30])
```


## Codebyte Example: Popping the 2 rightmost element from deque

This examples demonstrates the working of ```pop()```

```codebyte/python
from collections import deque

# Create a deque with some numbers
numbers = deque([1, 2, 3, 4, 5])

print("Initial deque:", numbers)

# Remove elements one by one from the right
last_item = numbers.pop()
print("Popped element:", last_item)
print("Deque after first pop:", numbers)

# Pop again
another_item = numbers.pop()
print("Popped another element:", another_item)
print("Final deque:" numbers)
```


Loading