I am learning Python and for practice I have written this function which sorts a list.
Can you please provide feedback on how I have structured it and what better ways could be there to write it for optimization/structuring?
originallist=[5, 4, 0, 3, 2,1] lowervalue=int() duplicatelist=originallist[:] ## This list is used to compare each element of the list with the first index of original list sortedlist=[] ## Sorted list while len(originallist)>0: # See if there are any elements left in list original list lowervalue=0.01 for i in duplicatelist: if originallist[0]<=i: temp=originallist[0] print('Temp: ', temp) elif originallist[0]>i: temp=i print('Temp: ', temp) if lowervalue>temp or lowervalue==0.01: lowervalue=temp print('LowerValue: ', lowervalue) sortedlist.append(lowervalue) print('Sorted List: ', sortedlist) duplicatelist.remove(lowervalue) print('Duplicate List: ', duplicatelist) originallist.remove(lowervalue) print('original List: ', originallist, ' \n') print(f'Sorted List :' , sortedlist)
Thank you!
originallist.sort()
) :)\$\endgroup\$list.sort()
orsorted
" is 'the best' but possibly not too helpful answer.\$\endgroup\$