Skip to content Skip to sidebar Skip to footer

Python Program Returning None Type Array When Append To The List

My python program has a minor problem. The case is that I'm trying to use myarray.append() to my array, but in the python shell, it's telling me this when I do a test appending in

Solution 1:

Your problem is in

l = l.sort()

The sort() method sorts a list in-place; the list itself is reordered, rather than returning a new list. The method returns None, which you are then assigning to l. So you just need to remove the assignment.

An alternative is to use

l = sorted(l)

which will actually make a copy of the original list with the elements in sorted order.

Solution 2:

In python, you can populate a list as follows with a list comprehension

l = [str(i) for i inrange(0, 1000)]
l > ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ..]

If you use sort in a list, it returns None which is an indicator in python, that it does the operation in place. So to print a sorted list, it's enough to do:

l.sort()
print(l)

Post a Comment for "Python Program Returning None Type Array When Append To The List"