in Education by
I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':(17, 205), 'orange':(1, 91)} I would like to find key, value pair according to max value of the second element in the tuple so in this case it should return: banana':(9, 407) I know I can get this result using lambda: key_max = max(inventory.keys(), key=(lambda k: inventory[k][1])) print key_max, inventory[key_max] However I try to receive same result using itemgetter() method as I read it is faster but I can only get data according to the value of first element of the tuple: from operator import itemgetter print max(inventory.iteritems(), key = itemgetter(1)) Is there a way I could get same result using itemgetter() method? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Although I don't see the point of this exercise... Here's a solution using only itemgetter and no lambdas, as was the original question: key = max(zip(inventory.keys(), map(itemgetter(1), inventory.values())), key=itemgetter(1))[0] print(key, inventory[key]) # banana (9, 407) Note that the above will only be efficient in Python 3.x where the built-in functions return iterators / view objects instead of lists. An equally efficient Python 2.x equivalent requires the use of library functions such as itertools.izip: # from itertools import izip, imap key = max(izip(inventory.iterkeys(), imap(itemgetter(1), inventory.itervalues())), key=itemgetter(1))[0] print key, inventory[key]

Related questions

0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    This is my Example list for you wich is stored in a textfile: DEVICE: Test HW-RELEASE: Test ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    This is my Example list for you wich is stored in a textfile: DEVICE: Test HW-RELEASE: Test ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This is my Example list for you wich is stored in a textfile: DEVICE: Test HW-RELEASE: Test ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This is my Example list for you wich is stored in a textfile: DEVICE: Test HW-RELEASE: Test ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    While finding the correct location for saving key value pair, how many times the key is hashed? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    The key-value pair data storages include all, except ________? In-memory Network Attached Storage Disk Storage Cache Storage All the options...
asked Sep 6, 2021 by JackTerrance
0 votes
    While SequenceFile stores each record as key-value pair, the avro system stored records as A - Simple text B - chained lists C - Linked lists D - schema and data...
asked Jan 13, 2021 in Technology by JackTerrance
0 votes
    Consider the impedance function Y(s)=(s^2+4s+3)/(3s^2+18s+24). Find the value of R0 after realizing ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function Y(s)=(s^2+4s+3)/(3s^2+18s+24). Find the value of R1 after realizing ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function Y(s)=(s^2+4s+3)/(3s^2+18s+24). Find the value of C1 after realizing ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function Y(s)=(s^2+4s+3)/(3s^2+18s+24). Find the value of R2 after realizing ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function Y(s)=(s^2+4s+3)/(3s^2+18s+24). Find the value of C2 after realizing ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
...