In this Python set program, we will find the index of an element in a set with the help of the below-given steps.
What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.
Index of an element in a set:
Steps to solve the program
1. Create a set using {}.
2. Add some elements in the set.
3. Convert the set to a list using list().
4. Find the index of an element in a set i.e. list using index() function.
5. Print the output.
Set = {1,2,4,6}
print("Set: ",Set)
List = list(Set)
print("The index of 4: ",List.index(4))
Output :
Set: {1, 2, 4, 6}
The index of 4: 2
Related Articles
Python program to convert a set to a dictionary with each element as key and value to an empty set.
Python program to create a set of even numbers from 1 to 20.
Python program to create a set of odd numbers from 1 to 20.
Python program to create a set of your favorite actors.
Python program to create a set of your favorite movies.
Python program to create two sets of books and find the intersection of sets.
convert a set to a dictionary