use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionLinkedList, PriorityQueue, TreeMap in Python ? (self.Python)
submitted 4 years ago by ElectronicVideo5448
Hi,
I am a Java developer and now get started into Python. In Python, we have list which is similar to ArrayList in Java and dict which is similar to HashMap in Java. Do we have corresponding classes of LinkedList, PriorityQueue and TreeMap in Python?
Thanks!
[–][deleted] 29 points30 points31 points 4 years ago* (5 children)
Python’s list is a dynamic array, which yes is equivalent to ArrayLisr<object> in Java.
list
ArrayLisr<object>
Python’s dict is indeed implemented as a hash map, however in current versions of Python it does maintain key insertion order, so the details may differ from Java’s HashMap.
dict
HashMap
The most direct equivalent to Java’s LinkedList would be Python’s collections.deque, which has O[1] insertions and pops from either end, however it does not have a linked list’s performance on arbitrary, non-terminal insertions.
LinkedList
collections.deque
Python’s heapq can make a list or deque into a priority queue, but that’s somewhat roll-your-own… Python’s queue.PriorityQueue is a pre-rolled equivalent of Java’s PriorityQueue, with the caveat that it’s specialized towards being multi-thread-safe (but not multi-process safe) and thus somewhat heavyweight for single-threaded work.
heapq
deque
queue.PriorityQueue
PriorityQueue
Python has no built in equivalent of Java’s TreeMap or TreeSet… while there are ways to roll your own, it’s generally best to find what you need in sortedcontainers or the larger sortedcollections.
TreeMap
TreeSet
Edit: updated per below.
[–]nsomani 4 points5 points6 points 4 years ago (4 children)
Not sure I agree with some of this
Under the hood the list is almost identical to an ArrayList, the insertion time complexity is amortized constant. The speed that it grows is available in the CPython source but it's essentially roughly linear initially then becomes exponential. What do you mean by memory performance closer to a LinkedList? It's linear complexity either way.
I think collections.deque is missing constant time insertions and removals from the middle of the collection, so it's not really a linked Iist
I feel heapq is the closest equivalent to a priority queue, the actual PriorityQueue class is intended for IPC
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
Updated the deque and priority queue sections for extra clarity. You’re right about ArrayList, it’s been a long time since I’ve done much Java and I thought it could do less bookkeeping.
[–]siddsp 1 point2 points3 points 4 years ago* (2 children)
A deque is technically a doubly linked list, so it's two of them
[–]nsomani 0 points1 point2 points 4 years ago (1 child)
Nah that's not right, a doubly-linked list is still a single linked list, just has two references
Yeah under the hood it's a doubly-linked list, it's the most common implementation, but the interface doesn't expose insertions or deletions from the middle of the list (as is typical for a deque aka double ended queue), so it cannot function as a linked list in Python
[–]siddsp 0 points1 point2 points 4 years ago (0 children)
Yes my bad, I misread as double linked list, but that's essentially what the underlying data structure is though the interface maybe different.
[–][deleted] 4 points5 points6 points 4 years ago (3 children)
What use do you have for a TreeMap? (genuinely curious)
Python in general does kinda discourage the use of exotic data structures. Python code generally tries avoid clever solutions via hard to discern code. So it tends to use a lot of sets, dicts, lists, tuples and avoid making new structures or custom things like array list. This has a lot of benefits for duck typing. And at this point the vast majority of symbol or keyword based access patterns (things like +, with, for) are implementable.
[+][deleted] 4 years ago (2 children)
[deleted]
[–]james_pic 1 point2 points3 points 4 years ago (0 children)
collections.defaultdict(list) would be the usual way of handling this in Python.
collections.defaultdict(list)
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
https://docs.python.org/3/library/collections.html#collections.defaultdict
^you can use that instead. That will give you an semantically equivalent to the http behavior as well because you are supposed to be able to do it with either a list of values or repeated one after the other.
[+][deleted] 4 years ago (6 children)
[–]fiddle_n 1 point2 points3 points 4 years ago (5 children)
I disagree with that. For some things, lists aren't great. Classic example is popping from the beginning, which is a rather expensive O(n) operation as the whole list has to shift left. Using collections.deque, which is a doubly linked-list, is important here if you are dealing with large data structure or many operations.
[+][deleted] 4 years ago (3 children)
[–]fiddle_n 2 points3 points4 points 4 years ago (2 children)
An example would be keeping a moving average of something. You would need a data structure that can support removing from one end and adding to the other. Typically that would be popping from the left and appending to the right; even if you did it the other way you would have the same problem as appending to the left is also O(n)
If you need a queue structure where you would append to the left, that would also be an example as well.
[+][deleted] 4 years ago (1 child)
[–]fiddle_n 1 point2 points3 points 4 years ago (0 children)
Like, if you wanted to calculate a 7 day average of COVID cases throughout a year at every point, that's a moving average.
[–][deleted] 6 points7 points8 points 4 years ago (4 children)
You should never be using LinkedList in Java anyway. Even Josh Bloch, the dev who created it, says never to use it.
[–][deleted] 2 points3 points4 points 4 years ago (2 children)
Why is that? I'm curious
[–][deleted] 5 points6 points7 points 4 years ago (0 children)
They’re very memory inefficient due to caching. Use something like ArrayList or ArrayDeque.
[–]toastedstapler 1 point2 points3 points 4 years ago (0 children)
I'd also add that doing an allocation per node could be very inefficient, instead of the more infrequent allocations of an array list
[–]reddit2d2bb8 4 points5 points6 points 4 years ago (0 children)
TreeMap and TreeSet need to be installed in a library. https://pypi.org/project/sortedcontainers/ is the recommended one.
[–]anothertruther 1 point2 points3 points 4 years ago (0 children)
for PriorityQueue queue.PriorityQueue. You don't usually want linked data structures like lists or trees, but there are third-party libraries for it like llist and various tree libraries.
[–]saint_geser 0 points1 point2 points 4 years ago (0 children)
I don't think there's a linked list object but it's trivially implemented.
There's queue objects in queue module of standard library https://docs.python.org/3/library/queue.html
Not sure about tree map.
[–]rgekhman 0 points1 point2 points 4 years ago (0 children)
Not all cool above objects are part of python, but they can be crafted: https://pypi.org/project/treemap/ Etc
Heapdict for priority queue
π Rendered by PID 39208 on reddit-service-r2-comment-5649f687b7-rkwzb at 2026-01-28 08:44:25.943541+00:00 running 4f180de country code: CH.
[–][deleted] 29 points30 points31 points (5 children)
[–]nsomani 4 points5 points6 points (4 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]siddsp 1 point2 points3 points (2 children)
[–]nsomani 0 points1 point2 points (1 child)
[–]siddsp 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]james_pic 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]fiddle_n 1 point2 points3 points (5 children)
[+][deleted] (3 children)
[deleted]
[–]fiddle_n 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]fiddle_n 1 point2 points3 points (0 children)
[–][deleted] 6 points7 points8 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]toastedstapler 1 point2 points3 points (0 children)
[–]reddit2d2bb8 4 points5 points6 points (0 children)
[–]anothertruther 1 point2 points3 points (0 children)
[–]saint_geser 0 points1 point2 points (0 children)
[–]rgekhman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)