44. Problem to find the index of an element in a set

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

convert a set to a dictionary

Leave a Comment