How do I return a list of the 3 lowest values in another list

后端 未结 5 1867
情话喂你
情话喂你 2021-01-13 21:00

How do I return a list of the 3 lowest values in another list. For example I want to get the 3 lowest values of this list:

in_list = [1, 2, 3, 4, 5, 6]
inpu         


        
5条回答
  •  青春惊慌失措
    2021-01-13 21:30

    You can use heapq.nsmallest:

    >>> from heapq import nsmallest
    >>> in_list = [1, 2, 3, 4, 5, 6]
    >>> nsmallest(3, in_list)
    [1, 2, 3]
    >>>
    

提交回复
热议问题