#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    28
    Rep Power
    0

    Sorting a list of lists by a particular element in each list


    I've got three lists:

    Code:
    list1 = [1, 7, 4, 9, 10]
    list2 = [3, 9, 2, 3, 3]
    list3 = [9, 2, 6, 3, 9]
    Then I make a list called list_of_lists, which contains the above three lists:

    Code:
    list_of_lists = [ [1, 7, 4, 9, 1],
                      [3, 9, 2, 3, 3],
                      [9, 2, 6, 3, 9] ]
    How do I sort the lists in list_of_lists by the second element in each list, so that I end up with this:

    Code:
    list_of_lists = [ [9, 2, 6, 3, 9],
                      [1, 7, 4, 9, 1],
                      [3, 9, 2, 3, 3] ]
    Is there a way to do this using the .sort() method?
    Is there a way to do this using sorted()?

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by Miktor
    Code:
    list_of_lists = [ [1, 7, 4, 9, 1],
                      [3, 9, 2, 3, 3],
                      [9, 2, 6, 3, 9] ]
    How do I sort the lists in list_of_lists by the second element in each list, so that I end up with this:

    Code:
    list_of_lists = [ [9, 2, 6, 3, 9],
                      [1, 7, 4, 9, 1],
                      [3, 9, 2, 3, 3] ]
    Is there a way to do this using the .sort() method?
    Is there a way to do this using sorted()?
    There is and itís fairly easy:

    Code:
    list_of_lists.sort(key=lambda x: x[1])
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    Explain


    SuperOscar, could you explain your code a little? What does the lambda mean?

    Thank You.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    As far as the search goes,

    list_of_lists.sort(key=lambda x: x[1])

    is equivalent to
    Code:
    def f(a):
        return a[1]
    
    list_of_lists.sort(key=f)
    So what's the difference? The version that defined f deposited the name "f" in some namespace. Another difference would be that if you need multiple statements to compute the key then, well, lambda allows for only one expression, not a statement.

    And of course that motivated the change in python3 for print becoming a function.

    lambda xrint x
    in python 2 does not work. whereas

    lambda xrint(x)
    does work in python 3.


    As for the other question, please learn to answer issues for yourself. Here's how:
    Code:
    >>> help(sorted)
    Help on built-in function sorted in module builtins:
    
    sorted(...)
        sorted(iterable, key=None, reverse=False) --> new sorted list
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo