How to sort a list of lists by a specific index of the inner list?












185















I have a list of lists. For example,



[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]


If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?










share|improve this question




















  • 6





    Link to tutorial: wiki.python.org/moin/HowTo/Sorting

    – Felix Kling
    Nov 13 '10 at 22:02






  • 3





    useful link: stackoverflow.com/questions/18142090/…

    – dot.Py
    Jun 6 '16 at 17:52
















185















I have a list of lists. For example,



[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]


If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?










share|improve this question




















  • 6





    Link to tutorial: wiki.python.org/moin/HowTo/Sorting

    – Felix Kling
    Nov 13 '10 at 22:02






  • 3





    useful link: stackoverflow.com/questions/18142090/…

    – dot.Py
    Jun 6 '16 at 17:52














185












185








185


56






I have a list of lists. For example,



[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]


If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?










share|improve this question
















I have a list of lists. For example,



[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]


If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?







python sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '10 at 22:00









John La Rooy

211k39276429




211k39276429










asked Nov 13 '10 at 21:54









oldspiceoldspice

993387




993387








  • 6





    Link to tutorial: wiki.python.org/moin/HowTo/Sorting

    – Felix Kling
    Nov 13 '10 at 22:02






  • 3





    useful link: stackoverflow.com/questions/18142090/…

    – dot.Py
    Jun 6 '16 at 17:52














  • 6





    Link to tutorial: wiki.python.org/moin/HowTo/Sorting

    – Felix Kling
    Nov 13 '10 at 22:02






  • 3





    useful link: stackoverflow.com/questions/18142090/…

    – dot.Py
    Jun 6 '16 at 17:52








6




6





Link to tutorial: wiki.python.org/moin/HowTo/Sorting

– Felix Kling
Nov 13 '10 at 22:02





Link to tutorial: wiki.python.org/moin/HowTo/Sorting

– Felix Kling
Nov 13 '10 at 22:02




3




3





useful link: stackoverflow.com/questions/18142090/…

– dot.Py
Jun 6 '16 at 17:52





useful link: stackoverflow.com/questions/18142090/…

– dot.Py
Jun 6 '16 at 17:52












9 Answers
9






active

oldest

votes


















258














This is a job for itemgetter



>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]


It is also possible to use a lambda function here, however the lambda function is slower in this simple case






share|improve this answer


























  • What if I would want to ignore case?

    – bzupnick
    Jul 29 '13 at 11:31






  • 5





    @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

    – John La Rooy
    Jul 29 '13 at 11:52











  • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

    – nidHi
    Dec 2 '16 at 9:48











  • Can I also get the indices of the sort, so to sort another related list of lists in the same order?

    – quarky
    Aug 22 '17 at 8:52











  • @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

    – John La Rooy
    Aug 23 '17 at 0:13



















132














in place



>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])


not in place using sorted:



>>> sorted(l, key=lambda x: x[2])





share|improve this answer





















  • 2





    Could you give more details about in place and not in place ?

    – qun
    Oct 10 '15 at 15:42






  • 7





    @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

    – John La Rooy
    Oct 15 '15 at 6:15











  • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

    – nidHi
    Dec 2 '16 at 9:48



















60














Itemgetter lets you to sort by multiple criteria / columns:



sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))





share|improve this answer



















  • 3





    I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

    – ZekeDroid
    Oct 9 '14 at 16:55



















8














Like this:



import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))





share|improve this answer































    5














    multiple criteria can also be implemented through lambda function



    sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))





    share|improve this answer































      5














      I think lambda function can solve your problem.



      old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

      #let's assume we want to sort lists by last value ( old_list[2] )
      new_list = sorted(old_list, key=lambda x: x[2])

      #Resulst of new_list will be:

      [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]





      share|improve this answer































        4














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


        You can easily sort using this snippet, where 1 is the index of the element.






        share|improve this answer































          0














          More easy to understand (What is Lambda actually doing):



          ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
          def thirdItem(ls):
          #return the third item of the list
          return ls[2]
          #Sort according to what the thirdItem function return
          ls2.sort(key=thirdItem)





          share|improve this answer































            0














            **old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
            #let's assume we want to sort lists by last value ( old_list[2] )
            new_list = sorted(old_list, key=lambda x: x[2])**


            correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4174941%2fhow-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              258














              This is a job for itemgetter



              >>> from operator import itemgetter
              >>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> sorted(L, key=itemgetter(2))
              [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]


              It is also possible to use a lambda function here, however the lambda function is slower in this simple case






              share|improve this answer


























              • What if I would want to ignore case?

                – bzupnick
                Jul 29 '13 at 11:31






              • 5





                @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

                – John La Rooy
                Jul 29 '13 at 11:52











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48











              • Can I also get the indices of the sort, so to sort another related list of lists in the same order?

                – quarky
                Aug 22 '17 at 8:52











              • @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

                – John La Rooy
                Aug 23 '17 at 0:13
















              258














              This is a job for itemgetter



              >>> from operator import itemgetter
              >>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> sorted(L, key=itemgetter(2))
              [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]


              It is also possible to use a lambda function here, however the lambda function is slower in this simple case






              share|improve this answer


























              • What if I would want to ignore case?

                – bzupnick
                Jul 29 '13 at 11:31






              • 5





                @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

                – John La Rooy
                Jul 29 '13 at 11:52











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48











              • Can I also get the indices of the sort, so to sort another related list of lists in the same order?

                – quarky
                Aug 22 '17 at 8:52











              • @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

                – John La Rooy
                Aug 23 '17 at 0:13














              258












              258








              258







              This is a job for itemgetter



              >>> from operator import itemgetter
              >>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> sorted(L, key=itemgetter(2))
              [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]


              It is also possible to use a lambda function here, however the lambda function is slower in this simple case






              share|improve this answer















              This is a job for itemgetter



              >>> from operator import itemgetter
              >>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> sorted(L, key=itemgetter(2))
              [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]


              It is also possible to use a lambda function here, however the lambda function is slower in this simple case







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '10 at 22:13

























              answered Nov 13 '10 at 21:59









              John La RooyJohn La Rooy

              211k39276429




              211k39276429













              • What if I would want to ignore case?

                – bzupnick
                Jul 29 '13 at 11:31






              • 5





                @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

                – John La Rooy
                Jul 29 '13 at 11:52











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48











              • Can I also get the indices of the sort, so to sort another related list of lists in the same order?

                – quarky
                Aug 22 '17 at 8:52











              • @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

                – John La Rooy
                Aug 23 '17 at 0:13



















              • What if I would want to ignore case?

                – bzupnick
                Jul 29 '13 at 11:31






              • 5





                @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

                – John La Rooy
                Jul 29 '13 at 11:52











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48











              • Can I also get the indices of the sort, so to sort another related list of lists in the same order?

                – quarky
                Aug 22 '17 at 8:52











              • @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

                – John La Rooy
                Aug 23 '17 at 0:13

















              What if I would want to ignore case?

              – bzupnick
              Jul 29 '13 at 11:31





              What if I would want to ignore case?

              – bzupnick
              Jul 29 '13 at 11:31




              5




              5





              @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

              – John La Rooy
              Jul 29 '13 at 11:52





              @bzupnick, use key=lambda x:x[2].casefold(). If your Python isn't new enough, just use .lower() instead of .casefold()

              – John La Rooy
              Jul 29 '13 at 11:52













              x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

              – nidHi
              Dec 2 '16 at 9:48





              x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?

              – nidHi
              Dec 2 '16 at 9:48













              Can I also get the indices of the sort, so to sort another related list of lists in the same order?

              – quarky
              Aug 22 '17 at 8:52





              Can I also get the indices of the sort, so to sort another related list of lists in the same order?

              – quarky
              Aug 22 '17 at 8:52













              @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

              – John La Rooy
              Aug 23 '17 at 0:13





              @quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.

              – John La Rooy
              Aug 23 '17 at 0:13













              132














              in place



              >>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> l.sort(key=lambda x: x[2])


              not in place using sorted:



              >>> sorted(l, key=lambda x: x[2])





              share|improve this answer





















              • 2





                Could you give more details about in place and not in place ?

                – qun
                Oct 10 '15 at 15:42






              • 7





                @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

                – John La Rooy
                Oct 15 '15 at 6:15











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48
















              132














              in place



              >>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> l.sort(key=lambda x: x[2])


              not in place using sorted:



              >>> sorted(l, key=lambda x: x[2])





              share|improve this answer





















              • 2





                Could you give more details about in place and not in place ?

                – qun
                Oct 10 '15 at 15:42






              • 7





                @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

                – John La Rooy
                Oct 15 '15 at 6:15











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48














              132












              132








              132







              in place



              >>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> l.sort(key=lambda x: x[2])


              not in place using sorted:



              >>> sorted(l, key=lambda x: x[2])





              share|improve this answer















              in place



              >>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
              >>> l.sort(key=lambda x: x[2])


              not in place using sorted:



              >>> sorted(l, key=lambda x: x[2])






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '10 at 22:06

























              answered Nov 13 '10 at 22:00









              mouadmouad

              46.1k139395




              46.1k139395








              • 2





                Could you give more details about in place and not in place ?

                – qun
                Oct 10 '15 at 15:42






              • 7





                @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

                – John La Rooy
                Oct 15 '15 at 6:15











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48














              • 2





                Could you give more details about in place and not in place ?

                – qun
                Oct 10 '15 at 15:42






              • 7





                @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

                – John La Rooy
                Oct 15 '15 at 6:15











              • x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

                – nidHi
                Dec 2 '16 at 9:48








              2




              2





              Could you give more details about in place and not in place ?

              – qun
              Oct 10 '15 at 15:42





              Could you give more details about in place and not in place ?

              – qun
              Oct 10 '15 at 15:42




              7




              7





              @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

              – John La Rooy
              Oct 15 '15 at 6:15





              @qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.

              – John La Rooy
              Oct 15 '15 at 6:15













              x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

              – nidHi
              Dec 2 '16 at 9:48





              x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?

              – nidHi
              Dec 2 '16 at 9:48











              60














              Itemgetter lets you to sort by multiple criteria / columns:



              sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))





              share|improve this answer



















              • 3





                I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

                – ZekeDroid
                Oct 9 '14 at 16:55
















              60














              Itemgetter lets you to sort by multiple criteria / columns:



              sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))





              share|improve this answer



















              • 3





                I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

                – ZekeDroid
                Oct 9 '14 at 16:55














              60












              60








              60







              Itemgetter lets you to sort by multiple criteria / columns:



              sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))





              share|improve this answer













              Itemgetter lets you to sort by multiple criteria / columns:



              sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 21 '13 at 10:25









              fiderfider

              1,1801520




              1,1801520








              • 3





                I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

                – ZekeDroid
                Oct 9 '14 at 16:55














              • 3





                I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

                – ZekeDroid
                Oct 9 '14 at 16:55








              3




              3





              I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

              – ZekeDroid
              Oct 9 '14 at 16:55





              I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!

              – ZekeDroid
              Oct 9 '14 at 16:55











              8














              Like this:



              import operator
              l = [...]
              sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))





              share|improve this answer




























                8














                Like this:



                import operator
                l = [...]
                sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))





                share|improve this answer


























                  8












                  8








                  8







                  Like this:



                  import operator
                  l = [...]
                  sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))





                  share|improve this answer













                  Like this:



                  import operator
                  l = [...]
                  sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '10 at 22:00









                  Jim BrissomJim Brissom

                  20.4k23032




                  20.4k23032























                      5














                      multiple criteria can also be implemented through lambda function



                      sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))





                      share|improve this answer




























                        5














                        multiple criteria can also be implemented through lambda function



                        sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))





                        share|improve this answer


























                          5












                          5








                          5







                          multiple criteria can also be implemented through lambda function



                          sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))





                          share|improve this answer













                          multiple criteria can also be implemented through lambda function



                          sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 24 '17 at 9:46









                          Rahul KumarRahul Kumar

                          79210




                          79210























                              5














                              I think lambda function can solve your problem.



                              old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

                              #let's assume we want to sort lists by last value ( old_list[2] )
                              new_list = sorted(old_list, key=lambda x: x[2])

                              #Resulst of new_list will be:

                              [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]





                              share|improve this answer




























                                5














                                I think lambda function can solve your problem.



                                old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

                                #let's assume we want to sort lists by last value ( old_list[2] )
                                new_list = sorted(old_list, key=lambda x: x[2])

                                #Resulst of new_list will be:

                                [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]





                                share|improve this answer


























                                  5












                                  5








                                  5







                                  I think lambda function can solve your problem.



                                  old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

                                  #let's assume we want to sort lists by last value ( old_list[2] )
                                  new_list = sorted(old_list, key=lambda x: x[2])

                                  #Resulst of new_list will be:

                                  [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]





                                  share|improve this answer













                                  I think lambda function can solve your problem.



                                  old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

                                  #let's assume we want to sort lists by last value ( old_list[2] )
                                  new_list = sorted(old_list, key=lambda x: x[2])

                                  #Resulst of new_list will be:

                                  [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 28 '17 at 13:26









                                  Tushar NirasTushar Niras

                                  821914




                                  821914























                                      4














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


                                      You can easily sort using this snippet, where 1 is the index of the element.






                                      share|improve this answer




























                                        4














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


                                        You can easily sort using this snippet, where 1 is the index of the element.






                                        share|improve this answer


























                                          4












                                          4








                                          4







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


                                          You can easily sort using this snippet, where 1 is the index of the element.






                                          share|improve this answer













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


                                          You can easily sort using this snippet, where 1 is the index of the element.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jun 20 '18 at 11:22









                                          Abhishek YadavAbhishek Yadav

                                          19019




                                          19019























                                              0














                                              More easy to understand (What is Lambda actually doing):



                                              ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
                                              def thirdItem(ls):
                                              #return the third item of the list
                                              return ls[2]
                                              #Sort according to what the thirdItem function return
                                              ls2.sort(key=thirdItem)





                                              share|improve this answer




























                                                0














                                                More easy to understand (What is Lambda actually doing):



                                                ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
                                                def thirdItem(ls):
                                                #return the third item of the list
                                                return ls[2]
                                                #Sort according to what the thirdItem function return
                                                ls2.sort(key=thirdItem)





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  More easy to understand (What is Lambda actually doing):



                                                  ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
                                                  def thirdItem(ls):
                                                  #return the third item of the list
                                                  return ls[2]
                                                  #Sort according to what the thirdItem function return
                                                  ls2.sort(key=thirdItem)





                                                  share|improve this answer













                                                  More easy to understand (What is Lambda actually doing):



                                                  ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
                                                  def thirdItem(ls):
                                                  #return the third item of the list
                                                  return ls[2]
                                                  #Sort according to what the thirdItem function return
                                                  ls2.sort(key=thirdItem)






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 23 '18 at 18:31









                                                  Maz1978Maz1978

                                                  315




                                                  315























                                                      0














                                                      **old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
                                                      #let's assume we want to sort lists by last value ( old_list[2] )
                                                      new_list = sorted(old_list, key=lambda x: x[2])**


                                                      correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?






                                                      share|improve this answer




























                                                        0














                                                        **old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
                                                        #let's assume we want to sort lists by last value ( old_list[2] )
                                                        new_list = sorted(old_list, key=lambda x: x[2])**


                                                        correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?






                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          **old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
                                                          #let's assume we want to sort lists by last value ( old_list[2] )
                                                          new_list = sorted(old_list, key=lambda x: x[2])**


                                                          correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?






                                                          share|improve this answer













                                                          **old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
                                                          #let's assume we want to sort lists by last value ( old_list[2] )
                                                          new_list = sorted(old_list, key=lambda x: x[2])**


                                                          correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 9 at 17:13









                                                          EgmontDeVosEgmontDeVos

                                                          11




                                                          11






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4174941%2fhow-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              404 Error Contact Form 7 ajax form submitting

                                                              How to know if a Active Directory user can login interactively

                                                              TypeError: fit_transform() missing 1 required positional argument: 'X'