
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access Kth Element in Set Without Deletion in Python
In Python, a set is an unordered collection of unique elements, represented by {}. It allows efficient membership testing and eliminates duplicate values, making it useful for tasks such as removing duplicates or checking for common elements between sets.
In this article, we will learn how to access the K element in a set without deletion in python.
Example
Assume we have taken an input set and K element. We will now find the index of that K element in an input set using the above methods.
Input
inputSet = {3, 9, 5, 1, 2, 8} k=5
Output
The index of given K{ 5 } in an input set: 3
In the above input set, the input K i.e, 5 is at the 3rd index in an input set. Hence the resultant k position in an input set is 3.
Methods Used
The following are the various methods to accomplish this task:
Using for loop
Using next() and iter() functions
Using list() and index() functions
Method 1: Using for loop
In this method we are just going to use a simple for loop to access the K element in set without deleting any element in it
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input set.
Print the input set.
Create another variable to store the input k value.
Assume the initial result position as -1(as the index starts from 0).
Use the for loop to traverse through each element of input set.
Increment the result position by 1 inside the loop.
Use the if conditional statement to check whether the current element is equal to the input k value.
Break the loop if the condition is true.
Print the resultant index of a given K element in an input set.
Example
The following program returns the index of a given K element in an input set using the for loop
# input set inputSet = {3, 9, 5, 1, 2, 8} # printing input set print("Input Set:", inputSet) # input k value k = 5 # initializing the result position as -1(as index starts from 0) resultPosition = -1 # traversing through each element of the input set for e in inputSet: # incrementing the result position by 1 resultPosition += 1 # checking whether the current element is equal to the k if e == k: # breaking the loop if the condition is true break # printing resultant index print("The index of given K{", k, "} in an input set:", resultPosition)
Output
On executing, the above program will generate the following output
Input Set: {1, 2, 3, 5, 8, 9} The index of given K{ 5 } in an input set: 3
Method 2: Using next() and iter() functions
In this method we are going to use next() and iter() function to access the k element in a set without deleting any other element
iter function()
Returns an iterator object. This function converts an iterable to an iterator.
Syntax
iter(object, sentinel)
Parameters
object(required): It is an iterable object.
sentinel(optional): It is a value that indicates the end of the sequence.
Return Value: returns an Iterator object
next() function
Returns the next item in an iterator like a list, string tuple, etc. If the iterable reaches its end, you can add a default return value to the return.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Use the iter() function to convert the input set to the iterator.
Use the for loop to traverse through the length of the input set using the len() function(returns the number of items in an object).
Incrementing the index/position of an input set to the next position using the next() function
Use the if conditional statement to check whether the current element is equal to the input k value.
Break the loop if the condition is true.
Print the resultant index of a given K element in an input set.
Example
The following program returns the index of a given K element in an input set using next() and iter() functions
# input set inputSet = {3, 9, 5, 1, 2, 8} # printing input set print("Input Set:", inputSet) # input k value k = 5 # converting input set to the iterator setIter = iter(inputSet) # traversing through the length of input set for i in range(len(inputSet)): # incrementing the index/position of set to next position using next() function e = next(setIter) # checking whether the current element is equal to the k if e == k: # breaking the loop if the condition is true break # printing resultant index print("The index of given K{", k, "} in an input set:", i)
Output
On executing, the above program will generate the following output
Input Set: {1, 2, 3, 5, 8, 9} The index of given K{ 5 } in an input set: 3
Method 3: Using list() and index() functions
In this example we are going to use a combination of list() and index functions of python to access the K element in a given set.
index() function
The position at the first occurrence of the provided value is returned by the index() function.
Syntax
list.index(element)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task
Use the list() function(converts the sequence/iterable to a list) to convert the input set into the list.
Use the index() function to get the index of input k value from the above list of set elements by passing k to it.
Print the resultant index of a given K element in an input set.
Example
The following program returns the index of a given K element in an input set using list() and index() functions -
inputSet = {3, 9, 5, 1, 2, 8} # printing input set print("Input Set:", inputSet) # input k value k = 5 # converting input set into list setElementsList =list(inputSet) # getting the index of input k value from the above list of set elements resultPosition = setElementsList.index(k) # printing resultant index print("The index of given K{",k,"} in an input set:", resultPosition)
Output
Input Set: {1, 2, 3, 5, 8, 9} The index of given K{ 5 } in an input set: 3
Conclusion
In this article we have learned 3 different methods to Access the K element in the set without deletion. We learned how to use the iter() function to loop through the given iterable and the next() function to go to the following position of it. Finally, we learned how to create a list from the given set and how to use the index() function to locate an element's index within the set.