What is IndexableSet in Python?
Overview
A set in Python is a linear data structure used to store elements without allowing duplicates. But it doesn’t allow indexing.
A set that allows numerical indexing of the elements is called IndexableSet and is provided by the sortedcollections module.
IndexableSet follows zero-based indexing.
Syntax
IndexableSet(iterable)
Parameter
iterable: This is an iterable of elements.
Return value
The method returns a set whose elements can be accessed via indexes.
Example
from sortedcollections import IndexableSetiterable = '85678756's = IndexableSet(iterable)print("Set - ", s)print("Elements from index 0 to 2 - ", s[:2])print("Element at index 3 - ", s[3])
Explanation
- Line 1: We import
IndexableSetfrom thesortedcollectionspackage. - Line 3: We define an iterable.
- Line 4: We create an instance of
IndexableSet()with the iterable defined in line 3. - Line 6: We print the set.
- Line 7: We print the elements from index
0to2. - Line 8: We print the element at index
3.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved