"Index" of an element in a set
EasySet Programs · 41 of 48
Write a Python program to find the position of an element in a set.
Input{30, 10, 20}, element 20 Approach
- Sets have no index. Sort into a list first, then use
index().
Solution
Python
s = {30, 10, 20}ordered = sorted(s)print(ordered, ordered.index(20))
NoteA set is unordered, so a position only means something after you convert it to a sorted list.
Learn the concept
New to this topic? Read the lesson, then come back.
More set programs
← All 48 Set Programs