1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Python code question

Discussion in 'Mac Programming' started by rien333, Jun 29, 2011.

  1. macrumors regular

    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:
    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:
    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.
  2. macrumors regular

    Look up the "sorted" built-in function. Your lambda function is going to look at the last item in the list:

    sorted(a_list, key = lambda x: x[-1] )
  3. macrumors regular

    Wow, thanks! I really appreciate your help!

Share This Page