What is the difference between a list and a deque in Python?

89 views

What is the difference between a list and a deque in Python?

What is the difference between a list and a deque in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

A deque (double-ended queue) allows efficient addition and removal of elements from both ends, unlike a list which is optimized for sequential access.

from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0)  # Adds 0 to the beginning
dq.append(4)  # Adds 4 to the end
print(dq)  # Output: deque([0, 1, 2, 3, 4])

solveurit24@gmail.com Changed status to publish February 13, 2025
0