Python program to print the smallest element in an array [3 Methods and 6+ Examples]

Do you want to find the smallest element in an array? In this NumPy tutorial, I will explain some Python program to print the smallest element in an array using different methods and some use cases.

To print the smallest element in an array in Python, we can use various methods such as the np.min() function from NumPy for efficient computation, sorting the array and selecting the first element, or manually iterating through the array with a for loop to find the minimum value.

Table of Contents

Python program to print the smallest element in an array

There are three different methods that Python program to print the smallest element in an array:

1. the np.min() Function
2. Sorting the Array
3. Using for loop

Let’s see them one by one using some illustrative examples:

1. To print the smallest element in an array using the np.min() function

The Python’s built-in min() function, directly returns the smallest element in the array. It’s the most straightforward and efficient way to find the smallest element.

Here, is the Python program to print the smallest element in an array:

``````import numpy as np

temperatures_nyc = np.array([58, 62, 67, 55, 60, 59, 61])
lowest_temperature = np.min(temperatures_nyc)
print("Lowest temperature in NYC this week:", lowest_temperature, "Fahrenheit")``````

Output:

``Lowest temperature in NYC this week: 55 Fahrenheit``

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

2. Find Smallest element in an array using Python using the sort() function

The array is first sorted using the sort() method, and then the first element is retrieved, as it will be the smallest after sorting. This method is useful when we also need the array in sorted order.

Here, is the Python program to print the smallest element in an array:

``````import numpy as np

book_prices_la = np.array([15.99, 17.99, 14.99, 16.49, 18.99])
book_prices_la.sort()
lowest_price = book_prices_la[0]
print("Lowest book price in Los Angeles:", lowest_price, "USD")``````

Output:

``Lowest book price in Los Angeles: 14.99 USD``

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

3. Python program to find smallest number in an array using for loop

The for loop method involves manually iterating through each element of the array and comparing them to find the smallest one. It is a more hands-on approach and can be customized for more complex conditions.

Here, is the Python program to print the smallest element in an array:

``````import numpy as np

tourists_chicago = np.array([120, 150, 95, 130, 110, 105, 98, 140])
least_visitors = tourists_chicago[0]
for visitors in tourists_chicago:
if visitors < least_visitors:
least_visitors = visitors
print("Least number of visitors in the museum:", least_visitors, "tourists")``````

Output:

``Least number of visitors in the museum: 95 tourists``

After executing the code in Pycharm, one can see the output in the below screenshot.

4. Find a minimum value in a 2D array

Here is a Python program to print the smallest element in an array:

``````import numpy as np
def find_smallest_in_2d_array(arr):
if arr.size == 0:
return "2D array is empty"
return np.min(arr)

elevation_data = np.array([
[5280, 5300, 5320],
[5400, 5380, 5360],
[5500, 5480, 5460]
])
print("Smallest elevation in 2D array:", find_smallest_in_2d_array(elevation_data))``````

Output:

``Smallest elevation in 2D array: 5280``

Upon running the code in Pycharm, the resulting output is captured in the screenshot provided below.

5. Program that prints the smallest element greater than x in a given unsorted array

To write a program that prints the smallest element greater than a given value x in an unsorted array, we need to follow these steps:

1. Take an input array and the value x.
2. Iterate through the array to find all elements greater than x.
3. Among these elements, find the smallest one.
``````import numpy as np
def smallest_greater_than_x(arr, x):
greater_elements = arr[arr > x]
if greater_elements.size == 0:
return "No element greater than x found"
return np.min(greater_elements)

populations = np.array([39.14, 28.7, 21.48, 19.45, 12.67])
x = 20
print("Smallest state population greater than", x, "million:", smallest_greater_than_x(populations, x))``````

Output:

``Smallest state population greater than 20 million: 21.48``

Below is a screenshot showcasing the result, following the implementation of the code in the Pycharm editor.

6. How to find the minimum element from two arrays in given time complexity in Python

Finding the minimum element from two arrays within a specific time complexity depends on the complexity we are targeting. For the most common scenarios, we’ll aim for an O(n) or O(nlogn) time complexity, where n is the total number of elements in both arrays.

``````import numpy as np
def find_minimum(arr1, arr2):
return np.min(np.concatenate((arr1, arr2)))

temperatures_LA = np.array([58.1, 57.5, 59.6, 58.3, 57.9])
temperatures_NY = np.array([31.2, 28.4, 27.9, 29.6, 30.1])
print("Minimum average January temperature:", find_minimum(temperatures_LA, temperatures_NY))``````

Output:

``Minimum average January temperature: 27.9``

Following the execution of the code in PyCharm, the resulting output is captured in the screenshot displayed below.

Conclusion

Here, I have explained four different approaches to find the smallest element in an array using Python: utilizing the np.min() function for a quick and efficient solution, sorting the array and picking the first element, and iterating through the array with a for loop. I have also explained different examples that illustrate the different ways to find the smallest element in an array with some conditions.

You may also like to read: