Hello and welcome back, in this Python solution article we will sort a number list with a Python function. If the function passes in an empty array or a none value then it should return an empty array or else it will sort the list and return the number list in ascending order!

Our strategy here is to compare the first number with the remaining numbers from the list and to put the smallest number in the list to the head of the list. Next we will compare the second, third and the remaining numbers with the numbers after it and placed the next smallest number in the correct position of the array.

def solution(nums): if nums == None: return [] for i in range(0, len(nums)): for j in range(i, len(nums)): if(nums[i] > nums[j]): temp = nums[i] nums[i] = nums[j] nums[j] = temp return nums

The sorting solution above is nothing different than the solution for Java or C++ so you should be very familiar with it.

Setting aside that sort() is already a method on lists (maybe you’re just trying to illustrate the algorithm), please ditch the use of the temp variable. Lines 9-11 can be replaced by:

nums[i], nums[j] = nums[j], nums[i]

Also, please change the tone of your blog posts to convey that you’re a beginner testing out ideas. No readers should be taking these posts as good examples of proper Python.