Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
I've been working on a project in Python today, but I can't find a solution for a problem I encountered a few hours ago. It would be great if I can finish my project tomorrow and I thought maybe someone around here is able to help me, because I just can't figure out a way to do the following thing myself:
Here is the code:
Code:
a_list =[[1,2,3,"I"],[4,7,"Cl"],[8,"Br"],[9,10,"F"]]
What I want, if it is possible at all, is that this list or a similar list becomes sorted alphabetically like this:
Code:
a_list = [[8,"Br"],[4,7,"Cl"],[9,10,"F"],[1,2,3,"I"]]
Hope I'm clear enough (For some reason I don't think I am...).
Hope one of your guys is able to help me! Or point me at some sort of direction.
 

ehoui

macrumors regular
Jan 27, 2011
217
0
Look up the "sorted" built-in function. Your lambda function is going to look at the last item in the list:

Code:
sorted(a_list, key = lambda x: x[-1] )
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.