The Python min() function is a built-in function that returns the smallest item in an iterable or the smallest of two or more arguments. It takes an iterable (such as a list, tuple, or set) or multiple arguments as input and returns the smallest value. If the iterable is empty, it raises a ValueError. The min() function can also take a key argument that specifies a function to be applied to each element before comparison. This allows for more complex comparisons, such as sorting a list of strings by their length. Overall, the min() function is a useful tool for finding the smallest value in a collection of data. Keep reading below to learn how to python min in Kotlin.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Python ‘min’ in Kotlin With Example Code

Python’s `min()` function is a built-in function that returns the smallest item in an iterable or the smallest of two or more arguments. In Kotlin, there is no direct equivalent to Python’s `min()` function, but we can achieve the same functionality using the `minBy()` function.

The `minBy()` function takes a lambda expression that returns a comparable value for each element in the collection. It then returns the element with the smallest comparable value.

Here’s an example of how to use `minBy()` to find the smallest element in a list of integers:


val numbers = listOf(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
val smallest = numbers.minBy { it }
println(smallest) // Output: 1

In this example, we create a list of integers and then use the `minBy()` function to find the smallest element in the list. The lambda expression `{ it }` returns each element in the list, and the `minBy()` function returns the element with the smallest value.

We can also use `minBy()` to find the smallest element in a list of custom objects. In this case, we need to provide a lambda expression that returns a comparable value for each object. For example, if we have a list of `Person` objects with `name` and `age` properties, we can find the youngest person like this:


data class Person(val name: String, val age: Int)

val people = listOf(
Person("Alice", 25),
Person("Bob", 30),
Person("Charlie", 20)
)

val youngest = people.minBy { it.age }
println(youngest?.name) // Output: Charlie

In this example, we create a list of `Person` objects and then use the `minBy()` function to find the youngest person. The lambda expression `{ it.age }` returns the age of each person, and the `minBy()` function returns the person with the smallest age.

In summary, while Kotlin doesn’t have a direct equivalent to Python’s `min()` function, we can achieve the same functionality using the `minBy()` function. We provide a lambda expression that returns a comparable value for each element in the collection, and the `minBy()` function returns the element with the smallest comparable value.

Equivalent of Python min in Kotlin

In conclusion, Kotlin provides a convenient and efficient way to find the minimum value in a collection using the `min()` function. This function can be used on any collection type, including lists, sets, and arrays. It takes advantage of Kotlin’s type inference system to determine the appropriate comparison function based on the element type. Additionally, the `minBy()` and `minWith()` functions provide even more flexibility for customizing the comparison logic. Overall, the `min()` function in Kotlin is a powerful tool for finding the smallest value in a collection with ease.

Contact Us