Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tuple + Lambda functions?

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)]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading