0

I am trying to reverse a list in python 2. The following Code 1 works. But the code 2 comes up with a Nonetype attribution error. What is wrong with using the .append method?

Code1

def list_reverse_right(my_list):
    if len(my_list) <= 1:
        return my_list
    else: 
        return list_reverse_right(my_list[1:]) + my_list[:1]

print list_reverse_right([2, 3, 1])

Code 2

def list_reverse_wrong(my_list):
    if len(my_list) <= 1:
        return my_list
    else: 
        return list_reverse_wrong(my_list[1:]).append(my_list[0])

print list_reverse_wrong([2, 3, 1])
  • 2
    `list_reverse_wrong(my_list[1:]).append(my_list[0])` reason for error `list.append` returns `None` it mutates list in place. Please go through docs. – Ch3steR Jun 08 '20 at 20:00
  • Whenever you see a `Nonetype` error in Python, look for the function call that returns `None`. That's what causes the error 99% of the time. – Mark Ransom Jun 08 '20 at 20:04
  • `AttributeError`, not "attribution error". – Karl Knechtel Sep 14 '22 at 15:05

0 Answers0