The Numpy library in Python comes with a number of useful functions and techniques to work with and manipulate the data in arrays. In this tutorial, we will look at how to get the k smallest values from a one-dimensional Numpy array (where k <= size of the array).

## How to get the k smallest values from a Numpy array?

To get the k smallest values from a Numpy array, first, sort the array (in ascending order) and then slice the first k values of the sorted array.

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

The following is the syntax –

# k smallest elements from numpy array ar np.sort(ar)[:k]

Here, the `np.sort()`

function returns a sorted copy of the array, we then slice this sorted array to get the first k values (which are the k smallest values since the array is sorted in ascending order).

## Steps to get the k smallest elements in a Numpy array

Let’s now look at a step-by-step example of using the above method. Here, we will create a numeric array of size 7 and get the 3 smallest elements in the array. That is k = 3.

**Upskill your career right now →**

### Step 1 – Create a Numpy array

First, we will create a Numpy array that we will be using throughout this tutorial.

import numpy as np # create numpy array ar = [3, 1, 5, 7, 4, 2, 6] # display the array print(ar)

Output:

[3, 1, 5, 7, 4, 2, 6]

Here, we used the `numpy.array()`

function to create a one-dimensional Numpy array of length 7 containing numeric values.

### Step 2 – Sort the array

Now, we will use the `numpy.sort()`

function to sort the array created above. This function returns a sorted copy of the original array and doesn’t modify it in-place.

# sort the array sorted_ar = np.sort(ar) # display the array print(sorted_ar)

Output:

[1 2 3 4 5 6 7]

The resulting array is sorted in ascending order.

Note – To sort a Numpy array, you can also use the instance method `numpy.ndarray.sort()`

(for example, `ar.sort()`

). This function sorts the array in-place.

**Upskill your career right now →**

### Step 3 – Slice the sorted array to get the k smallest elements

As the array is sorted in ascending order, the first k elements of the array are the k smallest elements in the array.

Slice the sorted array to get the first k elements (slice from the start of the array to k).

For example, let’s get the 3 smallest elements in the array (k=3).

# get the 3 smallest elements print(sorted_ar[:3])

Output:

[1 2 3]

We get the 3 smallest elements in the array.

You can combine the code for the last two steps in a single line and avoid the extra variable.

# get the 3 smallest elements print(np.sort(ar)[:3])

Output:

[1 2 3]

We get the same result as above.

### Summary – Get k smallest elements from a Numpy array

In this tutorial, we looked at how to get the k smallest elements from a Numpy array. The following is a short summary of the steps mentioned in this tutorial.

- Create a Numpy array (skip this step if you already have an array to operate on).
- Sort the Numpy array using the
`numpy.sort()`

function. - To get the k smallest elements in the array –
- Slice the first k elements of the sorted array if you sorted the array in ascending order.
- Slice the last k elements of the sorted array if you sorted the array in descending order.

You might also be interested in –

- Numpy – Get Max Value in Array
- Get the First N Rows of a 2D Numpy Array
- Get the First N Columns of a 2D Numpy Array

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**