My understanding is that key can accept functions as a parameter to sort, if I’m using [] to access the tuple. Why do I still get "Type Error: ‘Tuple’ object is not callable."?
list_ = [(34, 21, 56), (24, 12, 32), (42, 34, 42), (27, 11, 32)]
m = sorted(list_, key=(lambda x: x[1], list_))
print(m)
Expected output:
Sorted list based on second element of each tuple in the list.
[(27, 11, 32), (24, 12, 32), (34, 21, 56), (42, 34, 42)]
>Solution :
You messed up the sorted line. The entire section (lambda x:x[1], list_) is passed to the key parameter.
(lambda x:x[1], list_) is a tuple, and not callable.
Replace the line with this:
m = sorted(list_, key=lambda x:x[1])
and it should work.