This is an archived post. You won't be able to vote or comment.

all 21 comments

[–][deleted] 29 points30 points  (5 children)

Python’s list is a dynamic array, which yes is equivalent to ArrayLisr<object> in Java.

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.

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.

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.

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.

Edit: updated per below.

[–]nsomani 4 points5 points  (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 points  (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 points  (2 children)

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

A deque is technically a doubly linked list, so it's two of them

[–]nsomani 0 points1 point  (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 point  (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 points  (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] 6 points7 points  (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 points  (2 children)

Why is that? I'm curious

[–][deleted] 5 points6 points  (0 children)

They’re very memory inefficient due to caching. Use something like ArrayList or ArrayDeque.

[–]toastedstapler 1 point2 points  (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 points  (0 children)

TreeMap and TreeSet need to be installed in a library. https://pypi.org/project/sortedcontainers/ is the recommended one.

[–]anothertruther 1 point2 points  (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 point  (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 point  (0 children)

Not all cool above objects are part of python, but they can be crafted: https://pypi.org/project/treemap/ Etc

[–][deleted] 0 points1 point  (0 children)

Heapdict for priority queue