2

Hi so i have a list u = [2,45,0,56,78,13].

We can use min() method to find the minimum value in a list. I was going through the heap binary tree.

The python has a module heapq and there is a method heapq.nsmallest() which helps to find the n smallest number in a heap binary tree.

Does the default min() method in python use the heap binary tree to find the minimum value in the list?

Danish Xavier
  • 1,225
  • 1
  • 10
  • 21
  • 1
    No, it simply iterates through the sequence as you can see in the [source code](https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Python/bltinmodule.c#L1712) – Jan Wilamowski Jun 17 '21 at 07:37
  • So, is it more efficient to use heapq.nsmallest() for n =1 or default min(). – Danish Xavier Jun 17 '21 at 07:42
  • The heap has a setup cost that will only be worth it if you do many lookups. How many depends on the size of the list and is probably implementation dependent so you'll wanna measure the runtime and compare. A single lookup will no be worth it. – Jan Wilamowski Jun 17 '21 at 07:44

0 Answers0