27

Is there a built-in sorted dictionary implementation in Python 2.6, or are hashtables the only kind?

Clarifications:

  • I'm asking about sorted dictionarys, not ordered dictionaries!
Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • possible duplicate of [Key-ordered dict in python](http://stackoverflow.com/questions/1319763/key-ordered-dict-in-python) – wim Mar 19 '13 at 05:57

4 Answers4

11

I think the answer here is no.

There is a Treemap but it isn't in the python standard library.

http://pypi.python.org/pypi/treemap/

I think to those who don't like my answer, may think it's wrong due to a recent update. Please note this is for Python 2.6, not 2.7 or Python 3

Please add the correct answer if you still think it's wrong, or unhelpful, or at least give a reason why you think it's a bad answer.

James Hurford
  • 2,018
  • 19
  • 40
  • 4
    +1 it's funny how Python was supposed to have a really complete set of features, and yet I'm running across so many things it doesn't have built-in (various containers, do-while loops, switch statements, etc.). :( – user541686 Jul 30 '11 at 23:31
  • Yes the lack of do-while and switch statements were one of the things I wish were present in python, but have gotten used to their absence now, and don't even notice the lack of them any more. – James Hurford Jul 31 '11 at 12:29
  • Not sure on 3+ but 2.7 doesn't have it either. The give advice on how to make one here, but it still isn't there by default: http://docs.python.org/2/library/collections.html#ordereddict-examples-and-recipes – ShawnFumo Sep 03 '13 at 18:31
  • And the question was if there was a built-in sorted dictionary in Python 2.6 and the answer is no, regardless of whether one exists in Python 2.7 or Python 3+ – James Hurford Sep 24 '13 at 02:50
  • 1
    The link is actually to a python library that creates the Treemap visualization instead of a Treemap data structure – Llama.new Sep 17 '16 at 06:11
  • Quiet right, but the essential answer I gave is still correct. Maybe I should have left that library off my answer, but I didn't. And for python 2.6 the answer is still no. – James Hurford Nov 02 '16 at 16:09
6

Built-in no, as a third party package you can check blist , here is the doc for sorteddict.

mouad
  • 67,571
  • 18
  • 114
  • 106
2

I wrote a Python version of the Java TreeMap/TreeSet.

Source code and documentation can be accessed in this repo

You can install with pip install pytreemap. Tested for Python >=3.5

GavinPHR
  • 31
  • 1
  • 1
1

If by build-in you mean provided with a standard python install, the answer is no.

If you mean at the same level of implementation as Python's build-in dict then have a look at my C implementation of sorteddict and ordereddict: http://anthon.home.xs4all.nl/Python/ordereddict/

Anthon
  • 69,918
  • 32
  • 186
  • 246