{"id":6886,"library":"sortedcollections","title":"Python Sorted Collections","description":"Sorted Collections provides CPython-optimized mutable sorted collections (SortedList, SortedDict, SortedSet) that maintain their order automatically. As of version 2.1.0, it targets Python 3.7+ and is actively maintained, with releases typically following bug fixes or minor enhancements to ensure stability and performance.","status":"active","version":"2.1.0","language":"python","source_language":"en","source_url":"https://github.com/grantjenks/python-sortedcollections","tags":["collections","data-structures","sorted","ordered","list","dict","set","performance"],"install":[{"cmd":"pip install sortedcollections","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"SortedList","correct":"from sortedcollections import SortedList"},{"symbol":"SortedDict","correct":"from sortedcollections import SortedDict"},{"symbol":"SortedSet","correct":"from sortedcollections import SortedSet"}],"quickstart":{"code":"from sortedcollections import SortedDict\n\nsd = SortedDict()\nsd[5] = 'apple'\nsd[1] = 'banana'\nsd[3] = 'cherry'\n\nprint(f\"SortedDict items: {list(sd.items())}\")\n# Expected output: SortedDict items: [(1, 'banana'), (3, 'cherry'), (5, 'apple')]\n\n# Example with SortedList\nfrom sortedcollections import SortedList\n\nsl = SortedList([5, 1, 3, 2, 4])\nsl.add(0)\nprint(f\"SortedList: {list(sl)}\")\n# Expected output: SortedList: [0, 1, 2, 3, 4, 5]","lang":"python","description":"Demonstrates the creation and basic usage of SortedDict and SortedList, showing how elements are automatically kept in sorted order upon insertion."},"warnings":[{"fix":"Migrate to using the public API methods (e.g., `__getitem__`, `__len__`, `islice`, `irange`, `item_at`) instead of accessing internal attributes.","message":"Direct access to internal, non-public attributes like `_list` on `SortedList` or `SortedSet` was removed in version 2.0.0. Code that relied on these internal implementations will break.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If an element's sort-defining properties change, remove the element and then re-add it to the collection to ensure it is correctly positioned. For immutable elements, this is not an issue.","message":"Modifying mutable elements *in-place* within a `SortedList` or `SortedSet` can corrupt the collection's sort order if the modification changes the element's comparison value. The collection will not automatically re-sort.","severity":"gotcha","affected_versions":"All"},{"fix":"Be mindful of these performance characteristics, especially in performance-critical loops or with very large datasets. Profile your application to ensure sorted collections meet your performance needs.","message":"While highly optimized, many operations (e.g., insertion, deletion, lookup by value) on `SortedList`, `SortedSet`, and `SortedDict` have logarithmic time complexity (O(log N)) due to the necessity of maintaining sorted order. This differs from O(1) for some operations in standard, unsorted `list` or `dict`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'2.1.0':24 '3.7':28 'activ':31 'automat':20 'bug':37 'collect':3,5,12,47 'cpython':8 'cpython-optim':7 'data':49 'data-structur':48 'dict':54 'enhanc':41 'ensur':43 'fix':38 'follow':36 'list':53 'maintain':17,32 'minor':40 'mutabl':10 'optim':9 'order':19,52 'perform':46,56 'provid':6 'python':1,27 'releas':34 'set':55 'sort':2,4,11,51 'sorteddict':14 'sortedlist':13 'sortedset':15 'stabil':44 'structur':50 'target':26 'typic':35 'version':23","created_at":"2026-04-15T18:47:15.275981+00:00","updated_at":"2026-04-16T21:57:13.133285+00:00","problems":[{"fix":"pip install sortedcollections","cause":"The 'sortedcollections' package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'sortedcollections'"},{"fix":"from sortedcollections import SortedList","cause":"The class names in the 'sortedcollections' library are 'SortedList', 'SortedDict', and 'SortedSet' (title case), not lowercase.","error":"from sortedcollections import sortedlist"},{"fix":"Ensure all items inserted into the collection are of compatible types, or define comparison methods (__lt__, __eq__, etc.) for custom objects.","cause":"Items added to SortedList, SortedDict, or SortedSet must be mutually comparable using Python's default comparison operators.","error":"TypeError: '<' not supported between instances of 'type1' and 'type2'"},{"fix":"You do not need to call sort(); the SortedList is always kept in sorted order automatically.","cause":"SortedList automatically maintains its sorted order upon item insertion, so it does not have or require an explicit sort() method.","error":"AttributeError: 'SortedList' object has no attribute 'sort'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.1.0","cli_name":"","cli_version":null,"type":"library","homepage":"http://www.grantjenks.com/docs/sortedcollections/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/sortedcollections/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["data","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}