Home > Enterprise >  Python Lists - Create List, Sort, and Select First in One Line - Not Possible?
Python Lists - Create List, Sort, and Select First in One Line - Not Possible?

Time:10-10

Desired code:

SmallestNumber = [ 5, 7, 3 ].sort()[0]

This however does not work.

What does work:

Numbers = [ 5, 7, 3 ]
Numbers.sort()
SmallestNumber = Numbers[0]

Is there any way to do this in one line?

It seems that the issue is related to [list].sort() returning a NoneType.

It seems that the sorted() function allows me to do this:

Numbers = [ 5, 7, 3 ]
SmallestNumber = sorted( Numbers )[0]

This would work for me but I would prefer to accomplish this with a single line if possible.

CodePudding user response:

For more simplicity try builtin function:

min([ 5, 7, 3 ])

3

CodePudding user response:

SmallestNumber = sorted([ 5, 7, 3 ])[0]
  • Related