33

I know in c++ it already exist #include <list> Now I am curious to know if it exist in python also.

4
  • 3
    Welcome to SO! Could you elaborate why you need this? Python already has the list type. Commented Nov 3, 2013 at 10:55
  • 11
    A Python list is equivalent to an array, not a linked list, it's a different data type. Commented Nov 3, 2013 at 10:56
  • 3
    Possible duplicate of stackoverflow.com/questions/280243/python-linked-list Commented Nov 3, 2013 at 10:56
  • 1
    This question should be reopened - it's clear what the user is asking for. As @Leigh mentioned, a list in Python is something very different from a linked list. Maybe it's a duplicate, but the close reason given is wrong. Commented Aug 21, 2022 at 15:29

2 Answers 2

22

You can also take a look at llist python package, which provides some useful features that deque does not. There are not only doubly linked lists, but also single linked lists data structure in that package. IMHO, one of the biggest advantages of this package is the ability to store a reference to the llist elements.

Sign up to request clarification or add additional context in comments.

1 Comment

There's another answer here: stackoverflow.com/questions/280243/…
-1

It appears that collections.deque is a doubly-linked-list library in Python. According to the documentation, it should have approximately O(1) cost when appending or popping from the head or the tail, as well as O(n) for regular inserts (which matches what we'd expect from a linked list).

API: http://docs.python.org/2/library/collections.html#collections.deque

Source: https://stackoverflow.com/a/282238/2441252

8 Comments

I went through the document for deque. It seems like deque is more like a FIFO or LIFO. You cannot insert elements in the middle of the queue. You can only insert them at the beginning or the end.
@winni2k - only if you don't do something smart, like keep a reference to a particular node that represents a known insertion point. With a doubly linked list, you can get O(1) insertions to intelligently defined points and only pay an O(n) cost of finding those points up front.
i disagree, insert/remove should be O(1) for a linkedlist given a reference to the element
@Mugen True for python 2. But for Python 3.5, you can insert element in deque. See: docs.python.org/3.7/library/…
Linked lists should insert in O(1)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.