Member-only story
Python — Difference between list and deque
Let's understand the difference between a list and a deque
List is one of the Python data types and it's mutable. It stores all types of data in a sequenced manner. list is defined by having values between square brackets [].
Example:
list1 = [1, 'a', {'name': 'John'}]Deque(Double-ended queue) is a Python data structure that efficiently adds and removes elements from both sides. It is under the collection module and is an alternative to the list of scenarios where elements are frequently inserted and removed from both ends.
Example:
from collections import deque
deque_list = deque((1,2,3,4))
print(deque_list) #output : deque([1, 2, 3, 4]Random Access:
- List:
O(1) accessing an element by index, is pretty fast because accessing an element by the index to get the element. - Deque:
O(n) for accessing element by index, because it required to traverse from the end, Deque is implemented with doubt linked-list.
Memory Usage:
- List:
contiguous memory block, which can lead to over-allocation for resizing. - Deque:
Used smaller memory…






