Python – Find the indices for k Smallest elements
Sometimes, while working with Python lists, we can have a problem in which we wish to find the indices for K smallest elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let’s discuss a certain way to find indices of K smallest elements in list.
sorted() + lambda + list slicing
This task can be performed using the combination of above functions. In this the
sorted(), can be used to get the container in a way which requires to get K smallest elements at front end and then the indices can be computed using list slicing.
The original list is : [5, 6, 10, 4, 7, 1, 19] Indices list of min K elements is : [5, 3, 0]