What is lambda? How can you explain this sorting [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-7
















This question already has an answer here:




  • Syntax behind sorted(key=lambda: …)

    6 answers



  • Why are Python lambdas useful? [closed]

    26 answers




Please be gentle I'm still learning Python. I've an example where




pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]



pairs.sort(key=lambda pair: pair[1])




The result is




[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]




Can you please help explain why this is the sequence?










share|improve this question













marked as duplicate by Idlehands, coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

    – UnholySheep
    Jan 3 at 15:15











  • lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

    – Idlehands
    Jan 3 at 15:15








  • 1





    Give this post a look

    – yatu
    Jan 3 at 15:16













  • All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

    – chepner
    Jan 3 at 15:16













  • Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

    – Matina G
    Jan 3 at 15:18


















-7
















This question already has an answer here:




  • Syntax behind sorted(key=lambda: …)

    6 answers



  • Why are Python lambdas useful? [closed]

    26 answers




Please be gentle I'm still learning Python. I've an example where




pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]



pairs.sort(key=lambda pair: pair[1])




The result is




[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]




Can you please help explain why this is the sequence?










share|improve this question













marked as duplicate by Idlehands, coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

    – UnholySheep
    Jan 3 at 15:15











  • lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

    – Idlehands
    Jan 3 at 15:15








  • 1





    Give this post a look

    – yatu
    Jan 3 at 15:16













  • All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

    – chepner
    Jan 3 at 15:16













  • Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

    – Matina G
    Jan 3 at 15:18














-7












-7








-7









This question already has an answer here:




  • Syntax behind sorted(key=lambda: …)

    6 answers



  • Why are Python lambdas useful? [closed]

    26 answers




Please be gentle I'm still learning Python. I've an example where




pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]



pairs.sort(key=lambda pair: pair[1])




The result is




[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]




Can you please help explain why this is the sequence?










share|improve this question















This question already has an answer here:




  • Syntax behind sorted(key=lambda: …)

    6 answers



  • Why are Python lambdas useful? [closed]

    26 answers




Please be gentle I'm still learning Python. I've an example where




pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]



pairs.sort(key=lambda pair: pair[1])




The result is




[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]




Can you please help explain why this is the sequence?





This question already has an answer here:




  • Syntax behind sorted(key=lambda: …)

    6 answers



  • Why are Python lambdas useful? [closed]

    26 answers








python lambda






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 15:13









MilkyMilky

1




1




marked as duplicate by Idlehands, coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Idlehands, coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

    – UnholySheep
    Jan 3 at 15:15











  • lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

    – Idlehands
    Jan 3 at 15:15








  • 1





    Give this post a look

    – yatu
    Jan 3 at 15:16













  • All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

    – chepner
    Jan 3 at 15:16













  • Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

    – Matina G
    Jan 3 at 15:18



















  • A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

    – UnholySheep
    Jan 3 at 15:15











  • lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

    – Idlehands
    Jan 3 at 15:15








  • 1





    Give this post a look

    – yatu
    Jan 3 at 15:16













  • All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

    – chepner
    Jan 3 at 15:16













  • Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

    – Matina G
    Jan 3 at 15:18

















A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

– UnholySheep
Jan 3 at 15:15





A lambda is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions

– UnholySheep
Jan 3 at 15:15













lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

– Idlehands
Jan 3 at 15:15







lambda is the same as an unnamed function, i.e. def f(pair): return pair[1]. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...

– Idlehands
Jan 3 at 15:15






1




1





Give this post a look

– yatu
Jan 3 at 15:16







Give this post a look

– yatu
Jan 3 at 15:16















All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

– chepner
Jan 3 at 15:16







All functions are anonymous; the def statement simply binds the function to a name after it has been defined. A lambda expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.

– chepner
Jan 3 at 15:16















Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

– Matina G
Jan 3 at 15:18





Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.

– Matina G
Jan 3 at 15:18












1 Answer
1






active

oldest

votes


















0














A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:



pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

def key_func(pair):
return pair[1]

pairs.sort(key=key_func)


In this case, the lambda function is telling sort how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:



    pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

    def key_func(pair):
    return pair[1]

    pairs.sort(key=key_func)


    In this case, the lambda function is telling sort how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).






    share|improve this answer




























      0














      A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:



      pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

      def key_func(pair):
      return pair[1]

      pairs.sort(key=key_func)


      In this case, the lambda function is telling sort how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).






      share|improve this answer


























        0












        0








        0







        A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:



        pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

        def key_func(pair):
        return pair[1]

        pairs.sort(key=key_func)


        In this case, the lambda function is telling sort how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).






        share|improve this answer













        A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:



        pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

        def key_func(pair):
        return pair[1]

        pairs.sort(key=key_func)


        In this case, the lambda function is telling sort how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 15:19









        Harry CuttsHarry Cutts

        1,098921




        1,098921

















            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$