{"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":"en","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,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}