##### 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: 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: 3,081 Threads: 44 Joined: Jan 2018 Reputation: 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: Aug-07-2020, 02:45 PM Thanks a lot for the help, I feel progress! Reply deanhystad So-and-so of the Yard Posts: 2,136 Threads: 11 Joined: Feb 2020 Reputation: 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: 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 So-and-so of the Yard Posts: 2,136 Threads: 11 Joined: Feb 2020 Reputation: 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: 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 Create SQLite columns from a list or tuple? snakes 6 779 May-04-2021, 12:06 PM Last Post: snakes How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 717 Feb-28-2021, 10:38 PM Last Post: JeffDelmas Create variable and list dynamically quest_ 12 888 Jan-26-2021, 07:14 PM Last Post: quest_ How to create a linked list and call it? loves 12 1,069 Nov-22-2020, 03:50 PM Last Post: loves Struggling for the past hour to define function and call it back godlyredwall 2 574 Oct-29-2020, 02:45 PM Last Post: deanhystad Help with define a def function Omer_ 3 553 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 538 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 972 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 583 Jun-21-2020, 09:40 PM Last Post: jefsummers Cant define turtle color with function argument Wrightys99 2 715 Apr-22-2020, 01:43 PM Last Post: Wrightys99

Forum Jump:

### User Panel Messages

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