Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie lambda question
#1
I'm reading python tutorial and as an example of use of lambda there is this code:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
print(pairs)
I do not understand this code, hope that somebody can explain me the logic behind the output:
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Reply
#2
lambda are simple functions
code equal to
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def pair_sort(pair):
	return pair[1] # this point to second object in list (1, 'one')[1] == 'one' 

pairs.sort(key=pair_sort)
print(pairs)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
lambda pair: pair[1]
lambda is called an anonymous function. The argument is passed before the ":". The expression which returns some value is after the colons. In this case, this is pair[1] which points to the second element of an iterable. So the list will be sorted by the second element of the tuples.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Dec-10-2017, 06:33 AM)wavic Wrote:
lambda pair: pair[1]
lambda is called an anonymous function. The argument is passed before the ":". The expression which returns some value is after the colons. In this case, this is pair[1] which points to the second element of an iterable. So the list will be sorted by the second element of the tuples.

The second element of the iterable is "(2, 'two')"? Unfortunately, I still don't understand this order 4-1-3-2.
Reply
#5
(1, 'one')
(2, 'two')
(3, 'three')
(4, 'four')
The second element of all the tuple is

'one'
'two'
'three'
'four'
if you sort the above words, it would come out as

(4, 'four'), 
(1, 'one'), 
(3, 'three'), 
(2, 'two')
Reply
#6
As wavic said, list is sorted by second element of the tuples. In your list (iterable) they are strings. Strings are ordered lexically, so "four" is first one, as "f" appears in alphabet before the rest of the initials of other values ("one", "two" and "three").
Reply
#7
(Dec-10-2017, 04:54 PM)j.crater Wrote: As wavic said, list is sorted by second element of the tuples. In your list (iterable) they are strings. Strings are ordered lexically, so "four" is first one, as "f" appears in alphabet before the rest of the initials of other values ("one", "two" and "three").

This is what completely missed. Wall
Thank you all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 653 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 543 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 921 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,127 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  Question from complete python's newbie Davicom 3 2,305 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,446 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,402 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  newbie question....importing a created class ridgerunnersjw 5 2,570 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020