Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lambda function
#1
Hi, 

I have the following code:

from collections import OrderedDict
tallyDict = {'New York': 12, 'Boston': 32, 'Atlanta': 5, 'Los Angeles': 16, 'Miami': 21}
tallySorted = OrderedDict(sorted(tallyDict.items(), key=lambda t: t[1], reverse=True))
the part key=lambda t: t[1].

1) What does "t" here references?
I assume that "t" references tallyDict.items(). the square brackets in t[1] seems referencing list but type(tallyDict.items()), I got <class 'dict_items'>. So I'm a little confused here what "t" references.

2) Is there a technique that I could use to figure out myself what "t" in this lambda function references?

Thank you!
Reply
#2
The key function is passed each item of the sequence it is sorting.
In this case you are passing it the items() of a dict.
This is a sequence of (key, value) tuples like ('New York', 12).

So the argument t is one of these tuples and t[1] is the second element of the tuple.

IE you are sorting by the numbers and then reversing it.

Clear?
Reply
#3
Understood.

Thank you!
Reply
#4
lambda is just a short way of defining a function.  If you want a better idea of how it's called, use a better function :P
Here's an example, showing how you can figure out what the arguments are:
from collections import OrderedDict


def test_sort(*args):
   print(args)
   return 1


tallyDict = {'New York': 12, 'Boston': 32, 'Atlanta': 5, 'Los Angeles': 16, 'Miami': 21}
tallySorted = OrderedDict(sorted(tallyDict.items(), key=test_sort, reverse=True))
And the output is:
Output:
(('Miami', 21),) (('Boston', 32),) (('Los Angeles', 16),) (('New York', 12),) (('Atlanta', 5),)
So, one argument is passed, and it's a tuple of (key, value).  So in your lambda, when you return "t[1]", you're returning the second item in that tuple, which is... whatever number that represents for the city name.

Another way to test it out, is to see what some_dict.items() gives...
tallyDict = {'New York': 12, 'Boston': 32, 'Atlanta': 5, 'Los Angeles': 16, 'Miami': 21}
for thing in tallyDict.items():
   print(thing)
Output:
('Los Angeles', 16) ('Boston', 32) ('New York', 12) ('Miami', 21) ('Atlanta', 5)
When you saw " <class 'dict_items'>", that's because the items aren't actually calculated until they're needed, as it's lazy and only does work when you need it.  So looping over the values lets us see what they are.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 852 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Writing a lambda function that sorts dictionary GJG 1 1,989 Mar-09-2021, 06:44 PM
Last Post: buran
  translating lambda in function fabs 1 2,122 Apr-28-2020, 05:18 AM
Last Post: deanhystad
  Lambda function recursion error DeadlySocks 1 2,011 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  Lambda function error localsystemuser 3 4,661 Jan-29-2020, 01:43 PM
Last Post: DeaD_EyE
  Help with AWS Lambda function faws2019 0 1,557 Aug-27-2019, 01:54 PM
Last Post: faws2019
  Lambda function Uchikago 3 2,662 Jul-16-2019, 08:56 AM
Last Post: perfringo
  eval lambda function with restricted context Olivier 7 5,072 Mar-04-2019, 10:45 PM
Last Post: Olivier
  Using a lambda function within another function JChapman 8 5,341 Jan-08-2019, 01:54 PM
Last Post: JChapman
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,188 Apr-19-2018, 05:56 AM
Last Post: anandmn85

Forum Jump:

User Panel Messages

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