0

what I am trying to do here is append the element form the_list with the greatest value

in the [-1] position . i started by creating an index dictionary for the elements in the_list but i started to get lost in the logic flow.

the_list = [['a','b','c','1'],['b','c','e','4'],['d','e','f','2']]
D_indx_element = {}
D_indx_value = {}
output = []

temp = []
for i,k in zip(range(0,len(the_list)),the_list):
    D_indx_element[i] = k
    temp.append(int(k[-1]))
    D_indx_value[i] = int(k[-1])

in the end i would like to have:

output = [['b','c','e','4']]

since 4 is greater than 1 and 2

O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

1

Use max:

>>> the_list = [['a','b','c','1'],['b','c','e','4'],['d','e','f','2']]
>>> max(the_list, key=lambda x:int(x[-1]))
['b', 'c', 'e', '4']

Without lambda:

def func(x):
    return int(x[-1])
max(the_list, key=func)
#['b', 'c', 'e', '4']
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • hey do you by chance know of a good source to figure out what Lambda actually is doing besides the official documentation? I always see it in people's codes and it seems like a one line function type thing but i can't actually explain what it is to someone else – O.rka Oct 10 '13 at 19:21
  • @draconisthe0ry http://www.diveintopython.net/power_of_introspection/lambda_functions.html – Ashwini Chaudhary Oct 10 '13 at 19:25
  • 2
    @draconisthe0ry - `lambda` makes an inline function. It is no different than `def`, except that it can be used where `def` can't (as in the example above). Here is a reference: http://stackoverflow.com/questions/890128/python-lambda-why. Truth be told, Python can do just fine without `lambda`--it is simply syntax sugar. ;) –  Oct 10 '13 at 19:25