To be slightly more explicit, all of your lines look like the following: DLL_obj = DoublyLinkedList().append(24) That is, they create a new DLL, and then append (in this case) “24”. What you want is something like: myDLL = DoubleLinkedList() myDLL.append(24) myDLL.append(27) Now, you are appen… Answer from defjaf on discuss.python.org
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › doubly-linked-list-in-python
Doubly Linked List in Python - GeeksforGeeks
July 23, 2025 - Update the head of the list to point to the new node. ... # Python Program for a doubly linked list at the beginning of a node class Node: def __init__(self, data): self.data = data self.next = None self.prev = None # Function to insert a node at the beginning of a doubly linked list def insert_at_beginning(head, data): new_node = Node(data) new_node.next = head if head: head.prev = new_node return new_node # Function to display the elements of the doubly linked list def display(head): current = head while current: print(current.data, end=" <-> ") current = current.next print("None") # Driver Code head = None head = insert_at_beginning(head, 3) head = insert_at_beginning(head, 2) head = insert_at_beginning(head, 1) print("Doubly Linked List after insertion at the beginning:") display(head)
🌐
Python.org
discuss.python.org › python help
Building a DoublyLinkedList in Python - - append method - Python Help - Discussions on Python.org
January 8, 2024 - Greetings Pythonistas! I am learning how to write basic algorithms in Python but am taking an unusual approach. I am taking a Udemy course which teaches algorithms in JavaScript. What I do is watch the lesson where the …
Discussions

Inbuilt module implementing doubly linked list
Why doesn’t Python have an inbuilt module for linked lists? There must be one for it, just like a list in C++ STL. More on discuss.python.org
🌐 discuss.python.org
19
0
October 6, 2023
Freeing nodes in doubly linked list
Python's built in memory management is very good, it will clear out any unreferenced objects automatically. Of course I can't say for sure that your code will do that without actually seeing your code. Do you know that python has a doubley linked list with a maxlen arleady baked in? You don't need to brew your own. https://docs.python.org/3/library/collections.html#collections.deque More on reddit.com
🌐 r/learnpython
11
2
March 21, 2024
data structures - Doubly Linked list in Python - Stack Overflow
Hey guys I'm really lost. I am writing a Doubly Linked List program for my Data Structures class and I just can't figure it out. UPDATE: So I have my singly linked list assignment done. How can I More on stackoverflow.com
🌐 stackoverflow.com
Trying to implement an efficient LinkedList class in Python
Though in theory, it is supposed to be faster as it takes only O(N) complexity instead of O(N2) of list. i am going to challenge you on your Big O evaluation of python lists, worst case remove is O(n) not O(n2 ) there's also cases where the underlying memory will need to be re-allocated which you as a programmer are not in control of so a lot of the time measurements will depend on the history of the container. i looked at your other thread and i think you are on the right track but you need to keep track of the actual Node in the LinkedList where you are adding to/removing from. that way the add/remove is O(1) instead of the O(n) you get starting from the head every time. More on reddit.com
🌐 r/learnpython
13
12
April 17, 2024
🌐
Codecademy
codecademy.com › learn › linear-data-structures-python › modules › doubly-linked-lists-python › cheatsheet
Linear Data Structures: Doubly Linked Lists Cheatsheet | Codecademy
If the node exists, .remove_by_value() ... None as their defaults. ... Doubly linked lists in Python utilize an updated Node class that has a pointer to the previous node....
🌐
Drake
analytics.drake.edu › ~manley › CS66 › Spring2023 › notes › 2023-04-10-doubly-linked-lists
Doubly-Linked-Lists - CS 66: Introduction to Computer Science II
April 10, 2023 - Recall: when we made our Queue ADT using a Python list as the underlying data structure, one of enqueue and dequeue had to be $O(n)$. We can now use a doubly linked list instead, and all the operations will be $O(1)$.
🌐
DEV Community
dev.to › iamadhee › doubly-linked-lists-4c8o
Doubly Linked Lists - DEV Community
September 16, 2024 - Just like singly linked lists, a doubly linked list is a data structure that consists of a collection of nodes. Each node, in this case, is a separate object, but what makes it unique is that each node is linked not only to the next but also ...
🌐
W3Schools
w3schools.com › dsa › dsa_data_linkedlists_types.php
DSA Linked Lists Types
It takes up less space in memory because each node has only one address to the next node, like in the image below. A doubly linked list has nodes with addresses to both the previous and the next node, like in the image below, and therefore takes ...
Find elsewhere
🌐
Medium
medium.com › @momtajhossain2001 › doubly-linked-list-using-python-e0d34cd39c89
Doubly Linked List Using Python. A doubly linked list is a linear data… | by Momtaj Hossain Mow | Medium
October 30, 2024 - Doubly Linked List Using Python A doubly linked list is a linear data structure consisting of a set of nodes containing a value and two pointers, one pointing to the previous node in the list and …
🌐
Python.org
discuss.python.org › python help
Inbuilt module implementing doubly linked list - Python Help - Discussions on Python.org
October 6, 2023 - Why doesn’t Python have an inbuilt module for linked lists? There must be one for it, just like a list in C++ STL.
🌐
Codecademy
codecademy.com › article › doubly-linked-list
Doubly Linked List: Complete Guide & Implementation | Codecademy
This example implements a doubly linked list in Python, where each node stores data along with pointers to both the previous and next nodes. It includes methods to insert nodes at the beginning or end, delete nodes from the beginning, end, or ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python tutorial › python doubly linked list with examples
Python Doubly Linked List with Examples - Spark By {Examples}
May 31, 2024 - In this article, we will discuss how to implement the Doubly Linked list using dstructure module in python. Double Linked list is a one-dimensional data
🌐
Reddit
reddit.com › r/learnpython › freeing nodes in doubly linked list
r/learnpython on Reddit: Freeing nodes in doubly linked list
March 21, 2024 -

Hello, I have found it convenient to create a doubly linked list to iterate through a bunch of objects and store the them in order based on non numeric criteria.

I only care about the top 5 nodes so to eliminate the need to traverse a very long list, I want to just sever the nodes after 5.

My question is: do I have to delete the nodes somehow or can I just ground out the last node I care about? I have never really heard about memory management in python and want to make sure I’m not creating a leak and id prefer not to set all the nodes to none if I don’t have to

🌐
Educative
educative.io › answers › how-to-create-a-doubly-linked-list-in-python
How to create a doubly linked list in Python
First, we will create a node class, then we will use it to build a doubly linked list class. ... Doubly linked lists use the head variable to refer to the first element and the tail variable that refers to the last element of the list.
🌐
Pendancer
python.pendancer.com › Data_Structures › Doubly_Linked_List.html
Doubly Linked List - Python
One of the limitations of the singly linked list is that it can be traversed in only one direction that is forward. The doubly linked list has overcome this limitation by providing an additional pointer that points to the previous node. With the help of the previous pointer, the doubly linked list can be traversed in a backward direction thus making insertion and deletion operation easier to perform.
🌐
Dremendo
dremendo.com › python-programming-tutorial › python-doubly-linked-list
Doubly Linked List in Python Programming | Dremendo
Just like Singly Linked List, a Doubly Linked List in python is a user-defined data structure in which data is stored as an object called a node. Each node has three parts in a doubly linked list: the data, the previous, and the next.
🌐
Pynerds
pynerds.com › data-structures › doubly-linked-list-in-python
Doubly-Linked List in Python
Enter for a chance to win 1 kilogram of Snickers, Mars, Twix and Bounty chocolate bars. Free giveaway with worldwide shipping.
Top answer
1 of 2
2

Given the fact that the problem definition specifies "pointers", python is not a suitable language to implement this. But you can use python variables as "pointers" (or rather references) because that is what they are; A python variable is just a name for or refrence to an object.

But if you want to implement in python, I would use a list ot tuples.

The first one is a list of (name, weight) tuples.

In [1]: data = [("Michael", 275), ("Tom", 150), ("Abe", 200)]

The order in this list doesn't matter. Just append new tuples to this list as they arrive.

Now the easy way to do it would be to make shallow copies (which reference the same tuples), and sort them appropriately just before you print them;

In [2]: namelist = [d for d in data]

In [3]: namelist.sort(key=lambda x: x[0])

In [4]: namelist
Out[4]: [('Abe', 200), ('Michael', 275), ('Tom', 150)]

and

In [5]: weightlist = [d for d in data]

In [6]: weightlist.sort(key=lambda x: x[1])

In [7]: weightlist
Out[7]: [('Tom', 150), ('Abe', 200), ('Michael', 275)]

Printing these in the correct sequence is now trivial.

But this is expressly forbidden in the exercise. So what you have to do is something like this;

  • Create a new (name, weight) tuple
  • Walk the list of tuples sorted by weight and compare the weight in the new tuple with the weight in the existing tuple (hint: use enumerate so you get the index of the tuple in the list). As soon as you've found a weight that is greater than the weight of the listed tuple, insert the new tuple in the weight-sorted list.
  • similar for the name-sorted list, but then using the name as the compare value.

Something like this;

In [10]: newvalue = ("Eric", 225)

In [11]: for index, (name, weight) in enumerate(weightlist):
   ....:     if newvalue[1] < weight:
   ....:         weightlist.insert(index, newvalue)
   ....:         break
   ....:     

In [12]: weightlist
Out[12]: [('Tom', 150), ('Abe', 200), ('Eric', 225), ('Michael', 275)]

Note that this algorithm assumes that weightlist is already in sorted order!


Another solution that is more in line of the assignment would be to use a dictionary for every person;

In [37]: newvalue = {"name": "Eric", "weight": 225, "nextname": None, "nextweight": None}

You will also need the data list to hold all the dictionaries. And you will need two variables startname and startweight to hold the first name and lowest weight respectively.

After you have made a newvalue, you start with comparing newvalue["weight"] to startweight["weight"]. If the new weight is smaller than the startweight, then newvalue becomes the new startweight, and newvalue["nextweight"] should be set to the old startweight. If not, you move to the next item in the list and compare again. Note that if you want to insert in the chain, you have to change two nextweight attributes!

This is a double, singly linked list. Beginning with startweight and startname you can print both in order by walking both chains.

2 of 2
1

Here is how I would do it. I suggest that you create a new question or search around for how to read data from a text file.

class Node:
  def __init__(self, name, weight):
    self.name = name
    self.weight = weight
    self.prev_name = None
    self.next_name = None
    self.prev_weight = None
    self.next_weight = None


class DLL:
  def __init__(self):
    self.head = Node(None, None)
    self.tail = Node(None, None)
    self.head.next_name = self.tail
    self.head.next_weight = self.tail
    self.tail.prev_name = self.head
    self.tail.prev_weight = self.head

  def add(self, name, weight):
    node = Node(name, weight)

    # add by name
    p = self.head
    while (p.next_name != self.tail) and (p.next_name.name < name):
      p = p.next_name
    node.next_name = p.next_name
    node.prev_name = p
    p.next_name = node
    node.next_name.prev_name = node

    # add by weight
    p = self.head
    while (p.next_weight != self.tail) and (p.next_weight.weight < weight):
      p = p.next_weight
    node.next_weight = p.next_weight
    node.prev_weight = p
    p.next_weight = node
    node.next_weight.prev_weight = node

  def printByName(self):
    p = self.head
    while p.next_name != self.tail:
      print(p.next_name.name, p.next_name.weight)
      p = p.next_name

  def printByWeight(self):
    p = self.head
    while p.next_weight != self.tail:
      print(p.next_weight.name, p.next_weight.weight)
      p = p.next_weight
    return

And some results:

D = DLL()
D.add("Jim",150)
D.add("Tom",212)
D.add("Michael",174)
D.add("Abe",199)
D.printByName()
  Abe 199
  Jim 150
  Michael 174
  Tom 212
D.printByWeight()
  Jim 150
  Michael 174
  Abe 199
  Tom 212
🌐
Llego
llego.dev › home › blog › implementing a doubly linked list in python
Implementing a Doubly Linked List in Python - llego.dev
August 19, 2023 - Node - Each element in the linked list is called a node. A node contains the data being stored and pointers to the previous and next node. Head - This is the first node in the doubly linked list.
🌐
Udacity
udacity.com › blog › 2025 › 05 › how-to-implement-linked-lists-in-python-with-code-examples.html
How to Implement Linked Lists in Python: With Code Examples | Udacity
May 23, 2025 - We can represent it this way: Figure 1. Singly linked list structure with forward-only navigation · The second type, the doubly linked list, has nodes that point to both the next and previous nodes.
🌐
FavTutor
favtutor.com › blogs › doubly-linked-list-python
Doubly Linked List (Python Code with Example) | FavTutor
It is easier to implement Singly ... Doubly LinkedList, where every node takes an additional pointer to point the former node to the element in addition to the pointer for the next node....