Python .copy()

cheetah3051's avatar
Published Oct 22, 2025
Contribute to Docs

The .copy() method returns a shallow copy of a deque.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

copied_deque = original_deque.copy()

Parameters:

The .copy() method takes no parameters.

Return value:

Returns a new deque object that is a shallow copy of the original deque.

Example 1: Reference copy using assignment

The example shows that assigning one deque to another with = creates a reference, not a real copy:

from collections import deque
deque1 = deque([1, 2, 3, 4])
deque2 = deque1
print(deque1)
print(deque2)

The output of this code is:

deque([1, 2, 3, 4])
deque([1, 2, 3, 4])

However, modifications to deque2 affect deque1 since both refer to the same object.

Example 2: Creating a shallow copy

In this example, a separate deque is created using .copy(), leaving the original unchanged:

from collections import deque
orders = deque(['daisies', 'periwinkle'])
new_orders = orders.copy()
print(new_orders)
# Modify the new deque
new_orders.append('roses')
print(orders)
print(new_orders)

The output of this code is:

deque(['daisies', 'periwinkle'])
deque(['daisies', 'periwinkle'])
deque(['daisies', 'periwinkle', 'roses'])

The original orders deque remains unchanged when new_orders is modified.

Codebyte Example

The following example demonstrates the difference between assignment and .copy():

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours