Sitemap
Top Python Libraries

Python is widely used in fields such as data analysis, machine learning, and web development. Sharing these skills will help you advance further in your career.https://join.slack.com/t/aidisruptiont-9307882/shared_invite/zt-2vb2pzkqq-oTJmcTR_v0AWgJ31ZPfOGA

Member-only story

Python — Difference between list and deque

Let's understand the difference between a list and a deque

2 min readDec 17, 2024

--

photo by Author

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:

  1. List:
    O(1) accessing an element by index, is pretty fast because accessing an element by the index to get the element.
  2. 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:

  1. List:
    contiguous memory block, which can lead to over-allocation for resizing.
  2. Deque:
    Used smaller memory…

--

--

Top Python Libraries
Top Python Libraries

Published in Top Python Libraries

Python is widely used in fields such as data analysis, machine learning, and web development. Sharing these skills will help you advance further in your career.https://join.slack.com/t/aidisruptiont-9307882/shared_invite/zt-2vb2pzkqq-oTJmcTR_v0AWgJ31ZPfOGA