Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't know about dicts, but as far as I can tell, tuples give you all the generality, utility, and performance of lists.


A tuple, behind the scenes, is just a type tag, a refcount, a length, and an array of PyObject pointers. A list, instead of having a fixed array of PyObject pointers, has a pointer to a dynamically resizable array of them. Tuples and lists are both iterable, they both have O(1) indexing and fairly efficient internal representations, but the key difference is mutability. A tuple is supposed to be a composite data type; a list is a dynamic array that you store things in.

Aside from mutability, though, tuples can do pretty much anything lists can, and usually a bit more efficiently.


Performance? Unless I'm mistaken you can't update tuples efficiently.


How do you intend to "update" an immutable list? Isn't it... immutable?


Immutable lists can be preppended/popped in O(1)time, without affecting previous versions.

Immutable dicts can have stuff added and removed in O(log n) time, without affecting previous versions.

Scala's immutable vectors can have appending/prepending/concatenation/splitting/insertion/deletion in O(log n) time, without affecting previous versions. it's log base 32, so for any arrays using integer indexes, it'll never go above ~7, so it's basically constant.

Basically, you get (almost) the performance of mutable data structures, except you don't mess up the old versions of the thing every time you change something. It's really pretty incredible, the performance characteristics people manage to get with structural sharing: http://www.scala-lang.org/docu/files/collections-api/collect...


There's still a lookup hit of log-b32n versus contiguous memory immutable vector with O(1). Locality can also an issue with tree-like structures but muted with the large branch factor. Of course the flip side scala gives you the kitchen sink for persistance.


The Scala stuff is impressive. Updated docs at the new site: http://docs.scala-lang.org/overviews/collections/performance...

The new ConcurrentTrie is also a great addition to 2.10.


As a pure function where the returned value utilizes structural sharing with the input values.





Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: