Python .copy()
Published Oct 22, 2025
Contribute to Docs
The .copy() method returns a shallow copy of a deque.
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 dequedeque1 = deque([1, 2, 3, 4])deque2 = deque1print(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 dequeorders = deque(['daisies', 'periwinkle'])new_orders = orders.copy()print(new_orders)# Modify the new dequenew_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():
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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