Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is IndexableSet in Python?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 IndexableSet
iterable = '85678756'
s = IndexableSet(iterable)
print("Set - ", s)
print("Elements from index 0 to 2 - ", s[:2])
print("Element at index 3 - ", s[3])
Example using IndexableSet

Explanation

  • Line 1: We import IndexableSet from the sortedcollections package.
  • 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 0 to 2.
  • Line 8: We print the element at index 3.

RELATED TAGS

python

CONTRIBUTOR

Abhilash
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring