{"id":672,"library":"ordered-set","title":"OrderedSet","description":"An OrderedSet is a custom MutableSet that remembers its order, so that every entry has an index that can be looked up. It combines the uniqueness of a set with the order-preserving and indexable properties of a list. The library is currently at version 4.1.0 and is actively maintained, with updates driven by new features and bug fixes.","status":"active","version":"4.1.0","language":"python","source_language":"en","source_url":"https://github.com/rspeer/ordered-set","tags":["set","ordered","collection","datastructure","hybrid","indexing"],"install":[{"cmd":"pip install ordered-set","lang":"bash","label":"Install stable release"}],"dependencies":[],"imports":[{"note":"There are multiple packages with similar names. Ensure you are importing from 'ordered_set' (underscore) as installed by 'pip install ordered-set' (hyphen).","wrong":"from orderedset import OrderedSet","symbol":"OrderedSet","correct":"from ordered_set import OrderedSet"}],"quickstart":{"code":"from ordered_set import OrderedSet\n\n# Create an OrderedSet\nletters = OrderedSet('abracadabra')\nprint(f\"Initial OrderedSet: {letters}\")\n# Expected: OrderedSet(['a', 'b', 'r', 'c', 'd'])\n\n# Check for membership\nprint(f\"'r' in letters: {'r' in letters}\")\n# Expected: 'r' in letters: True\n\n# Get item by index\nprint(f\"letters[2]: {letters[2]}\")\n# Expected: letters[2]: r\n\n# Get index of an item\nprint(f\"letters.index('r'): {letters.index('r')}\")\n# Expected: letters.index('r'): 2\n\n# Add a new item (returns index)\nnew_index = letters.add('x')\nprint(f\"After adding 'x': {letters}, index returned: {new_index}\")\n# Expected: After adding 'x': OrderedSet(['a', 'b', 'r', 'c', 'd', 'x']), index returned: 5\n\n# Set operations\nmore_letters = OrderedSet('shazam')\nletters |= more_letters\nprint(f\"Union with 'shazam': {letters}\")\n# Expected: Union with 'shazam': OrderedSet(['a', 'b', 'r', 'c', 'd', 'x', 's', 'h', 'z', 'm'])\n","lang":"python","description":"Demonstrates basic creation, membership testing, indexing, adding elements, and set operations with OrderedSet."},"warnings":[{"fix":"Use `OrderedSet` when you need both set semantics (uniqueness, set operations) and explicit order-based integer indexing/slicing.","message":"Python's built-in `dict` (since 3.7) maintains insertion order for its keys. However, `OrderedSet` provides the full `MutableSet` API along with list-like integer indexing and slicing, which `dict` keys alone do not. Do not assume `dict.fromkeys()` provides equivalent functionality for all use cases.","severity":"gotcha","affected_versions":"All versions on Python >= 3.7"},{"fix":"Benchmark your specific use case if deletion performance is critical. For most scenarios, the O(1) operations are sufficient.","message":"The `OrderedSet` implementation prioritizes O(1) performance for most operations (insertion, iteration, membership testing, index lookup) but deletion is O(N). If your primary use case involves frequent deletions from large sets, consider alternative data structures or performance implications.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of the return value of `.add()`. If you need to check if an item was newly added, you might compare the length before and after, or check `item not in my_set` before adding.","message":"The `.add()` method of `OrderedSet` returns the integer index of the added item (or its existing index if already present), unlike the standard `set.add()` method which always returns `None`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `pip install ordered-set` and `from ordered_set import OrderedSet`. Verify the documentation for the specific package you intend to use.","message":"There are multiple Python packages with similar names (e.g., `orderedset` (lowercase), `ordered-set-37`, `orderly-set`, or `sortedcollections.OrderedSet`). Ensure you install `ordered-set` (hyphenated) and import `OrderedSet` from `ordered_set` (underscore) to use this specific library. Different packages may have different APIs, features, and maintenance statuses.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T17:43:16.265Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure the library is installed using pip and the import statement is correct. The package name on PyPI is 'ordered-set', but the module name for import is 'ordered_set'.\n```python\npip install ordered-set\nfrom ordered_set import OrderedSet\n```","cause":"This error occurs when the 'ordered-set' library is not installed in the Python environment, or is installed but the import statement uses an incorrect module name.","error":"ModuleNotFoundError: No module named 'ordered_set'"},{"fix":"If you need to store `OrderedSet` instances in another set or use them as dictionary keys, convert them to an immutable (hashable) representation first, such as a tuple of their elements, or use a `frozenset` if order is not critical for the inner elements.\n```python\nfrom ordered_set import OrderedSet\n\nos1 = OrderedSet()\nos2 = OrderedSet()\n\n# Incorrect: Trying to put mutable OrderedSets into a set\n# my_set_of_orderedsets = {os1, os2} # This would raise the TypeError\n\n# Correct: Convert to tuple for hashability\nmy_set_of_tuples = {tuple(os1), tuple(os2)}\nprint(my_set_of_tuples)\n\n# Correct: If order isn't critical for the inner set, use frozenset\n# my_set_of_frozensets = {frozenset(os1), frozenset(os2)}\n# print(my_set_of_frozensets)\n```","cause":"Python's built-in `set`s and dictionary keys require their elements to be 'hashable' (immutable). An `OrderedSet` object is mutable, meaning its contents can change after creation, and therefore it is not hashable. This error occurs when trying to add an `OrderedSet` as an element to another `set` or `OrderedSet`, or use it as a key in a dictionary.","error":"TypeError: unhashable type: 'OrderedSet'"},{"fix":"Convert the mutable `list`s to immutable `tuple`s before adding them to the `OrderedSet`. Tuples are hashable and can be members of sets.\n```python\nfrom ordered_set import OrderedSet\n\nmy_list =\nmy_another_list =\n\nmy_ordered_set = OrderedSet()\n\n# Incorrect: Adding a list directly\n# my_ordered_set.add(my_list) # This would raise the TypeError\n\n# Correct: Add a tuple instead\nmy_ordered_set.add(tuple(my_list))\nmy_ordered_set.add(tuple(my_another_list))\nprint(my_ordered_set)\n```","cause":"Similar to the `OrderedSet` itself, mutable objects like Python `list`s cannot be elements of a `set` (or `OrderedSet`) because their contents can change, which would invalidate their hash value. This error occurs when you attempt to add a `list` directly into an `OrderedSet`.","error":"TypeError: unhashable type: 'list'"},{"fix":"Update your code or the problematic dependency to import `MutableSet` from `collections.abc` instead of `collections`.\n```python\n# Incorrect import (for Python 3.3+):\n# from collections import MutableSet\n\n# Correct import:\nfrom collections.abc import MutableSet\n\n# Example usage (if defining a custom class that needs MutableSet):\nclass MyCustomSet(MutableSet):\n    # ... implementation ...\n    pass\n```","cause":"This error typically occurs in Python versions 3.3 and later when code attempts to import `MutableSet` (or other Abstract Base Classes) directly from the `collections` module. These ABCs were moved to `collections.abc` in Python 3.3. While `ordered-set` (v4.1.0) correctly imports from `collections.abc` internally, this error can arise if a user's own code or another dependency tries to subclass or reference `collections.MutableSet` when interacting with or expecting an `OrderedSet`.","error":"AttributeError: module 'collections' has no attribute 'MutableSet'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"4.1.0","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"17.8M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0,"mem_mb":0.5,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0.5,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.7,"disk_size":"19.6M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.7,"disk_size":"19.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"11.5M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"11.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"11.3M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"17.3M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}