##### How to define a function to create a resorted list?
 How to define a function to create a resorted list? sparkt Programmer named Tim Posts: 17 Threads: 6 Joined: Aug 2020 Reputation: 0 Aug-06-2020, 06:33 PM After day-long trial-and-error and research I found the code from web which does what I tried to do, i.e. resorting a list according to absolute difference to a value. ```def sortleastdev(a, val): b =[[0 for x in range(2)] for y in range(len(a))] for i in range(0, len(a)): b[i][0] = abs(a[i]-val) b[i][1] = i b.sort() for i in range(0, len(a)): print(a[b[i][1]]) a = [7, 12, 2, 4, 8, 0] val = 6 sortleastdev(a, val) from statistics import mean L = [1.238623532,1.315924461,1.430787909,0.65436604,0.78646411,1.551692625,1.143410924,1.044302349,1.12971696,1.007285185,1.009553518,0.646888596,1.027950548,0.950471257,1.048221271,1.070840989] sortleastdev(L, mean(L)) ```But what if I wish to define "sortleastdev" as a new list? How to make it giving me a new resorted list instead, where "sortleastdev(L, mean(L))[0]" would be 1.070840989? I'm sure it's a very newbie question. I tried to search through the web without progress though. Much appreciated. Reply Posts: 2,658 Threads: 31 Joined: Jan 2018 Reputation: 250 Aug-06-2020, 07:40 PM You only need to return the list instead of printing it ```def sortleastdev(a, val): b =[[0 for x in range(2)] for y in range(len(a))] for i in range(0, len(a)): b[i][0] = abs(a[i]-val) b[i][1] = i b.sort() return [a[b[i][1]] for i in range(len(a))] ... result = sortedleastdev(L, mean(L)) ```Functions that return values are much more useful than functions that print values. Reply sparkt Programmer named Tim Posts: 17 Threads: 6 Joined: Aug 2020 Reputation: 0 Aug-07-2020, 02:45 PM Thanks a lot for the help, I feel progress! Reply deanhystad Da Bishop Posts: 1,056 Threads: 5 Joined: Feb 2020 Reputation: 124 Aug-07-2020, 05:13 PM Why not use a sorting key? Reply sparkt Programmer named Tim Posts: 17 Threads: 6 Joined: Aug 2020 Reputation: 0 Aug-07-2020, 05:21 PM (Aug-07-2020, 05:13 PM)deanhystad Wrote: Why not use a sorting key? How does that work? Further info would be appreciated. Reply deanhystad Da Bishop Posts: 1,056 Threads: 5 Joined: Feb 2020 Reputation: 124 Aug-07-2020, 05:26 PM (This post was last modified: Aug-07-2020, 05:27 PM by deanhystad.) Look up sorting keys in the python documentation. They even provide examples. I wrote a 3 line script to sort you list by deviation from the mean. Used the standard sorted() call and used the key to convert values in the list to a deviation value. https://docs.python.org/3/howto/sorting.html Reply sparkt Programmer named Tim Posts: 17 Threads: 6 Joined: Aug 2020 Reputation: 0 Aug-08-2020, 04:10 PM (Aug-07-2020, 05:26 PM)deanhystad Wrote: Look up sorting keys in the python documentation. They even provide examples. I wrote a 3 line script to sort you list by deviation from the mean. Used the standard sorted() call and used the key to convert values in the list to a deviation value. https://docs.python.org/3/howto/sorting.html Great to learn about that. It does simplify a lot! Thanks very much. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Help with define a def function Omer_ 3 139 Sep-20-2020, 06:59 PM Last Post: Omer_ How to create and define in one line a 2D list of class objects in Python T2ioTD 1 182 Aug-14-2020, 12:37 PM Last Post: Yoriz How to make this function general to create binary numbers? (many nested for loops) dospina 4 395 Jun-24-2020, 04:05 AM Last Post: deanhystad Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 271 Jun-21-2020, 09:40 PM Last Post: jefsummers Cant define turtle color with function argument Wrightys99 2 325 Apr-22-2020, 01:43 PM Last Post: Wrightys99 create function let_to_num() al_Czervik 2 320 Apr-17-2020, 10:44 PM Last Post: al_Czervik Create a program that PING a list of IPs skaailet 7 560 Mar-26-2020, 10:46 PM Last Post: snippsat Create a list of co-ordinates from Mouse press events Jagruthi 0 292 Mar-12-2020, 08:46 AM Last Post: Jagruthi create a list of object with a list of character studenthch 0 301 Feb-12-2020, 08:43 AM Last Post: studenthch Create function around sql ayomayam 1 355 Feb-05-2020, 07:20 PM Last Post: ayomayam

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020