简体   繁体   中英

TypeError: 'NoneType' object is not iterable. Why?

students = [("Tom", "F"),
            ("Henry", "A"),
            ("Patrick","D")]

grade = lambda students:students[1]
sorted_students = students.sort(key=grade)

for i in sorted_students:
    print(i)

This program returns "TypeError: 'NoneType' object is not iterable", and I'd like to know why. I know that the value of sorted_students is None, but how did it get this value?

students.sort() does not return a sorted copy of the list, it just sorts in place students . Instead, you should use sorted_students = sorted(students) , or simply reuse students after having sorted it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM