Appending a range to a list appends "range(1, 3)"


Here is the function:
def append_size(lst):
length = len(lst)
lst.append(range(1, length))
return lst
Here is the error message from Codecademy:
append_size([23, 42, 108]) should have returned [23, 42, 108, 1, 2, 3] , and it returned [23, 42, 108, range(1, 3)]
What is going on here? What can I do to fix the issue?


is this python3? range() does not produce a list in python3, you could cast to list of course.


Please post a link to the exercise, so that we can investigate this problem.


range() is in fact it’s own type of object (class) in Python3, as Stetim94 pointed out! So when you are appending range() to the list, it is adding the object itself, not the numbers in the range you specified. One way to get around this is to use the inbuilt Python list() function, which expands the range() object into a list. However, beware that .append() is only for one element at a time!

Some alternative ways you might want to consider is using a for loop with the range. You could also make a list out of the range() object as described above, and use .extend() on your list. A third way could be simply using addition, as Python allows you to add lists to other list in this manner:

my_list += ["these", "elements", "will", "be", "added"]