I have a .py
file that takes a list, finds the lowest number, puts it into a new array, removes the lowest number from the first array, and repeats until the original array returns contains no more items:
def qSort(lsort):
listlength = len(lsort)
sortedlist = list()
if listlength == 0:
return lsort
else:
while listlength > 0:
lmin = min(lsort)
sortedlist.append(lmin)
lsort.remove(lmin)
listlength = len(lsort)
return sortedlist
Now another .py
file imports the qSort
and runs it on some list, saving it to a variable. Then I try to use the .reverse()
command on the list and I end up getting it as a NoneType
. I try to use reversed()
, but all it does is say "<listreverseiterator object at 0xSomeRandomHex>"
:
from qSort import qSort #refer to my first Pastebin
qSort = qSort([5,42,66,1,24,5234,62])
print qSort #this prints the sorted list
print type(qSort) #this prints <type 'list'>
print qSort.reverse() #this prints None
print reversed(qSort) #this prints "<listreverseiterator object at 0xSomeRandomHex>"
Can anyone explain why I can't seem to reverse the list, no matter what I do?
reverse() method returns None because it reverses the list in place. It doesn't return a new reversed list.
Python lists can be reversed in-place with the list. reverse() method. This is a great option to reverse the order of a list (or any mutable sequence) in Python. It modifies the original container in-place which means no additional memory is required.
The reverse() method doesn't return any value. It updates the existing list.
Python provides a builtin function reversed() i.e. The reversed() function returned a reverse iterator of the given list and then we passed this reverse iterator to the list() function, which iterated over all the elements of the list in reverse order and inserted them to a new list i.e. a list with reversed contents.
As jcomeau mentions, the .reverse()
function changes the list in place. It does not return the list, but rather leaves qSort
altered.
If you want to 'return' the reversed list, so it can be used like you attempt in your example, you can do a slice with a direction of -1
So replace print qSort.reverse()
with print qSort[::-1]
You should know slices, its useful stuff. I didn't really see a place in the tutorial where it was all described at once, (http://docs.python.org/tutorial/introduction.html#lists doesn't really cover everything) so hopefully here are some illustrative examples.
Syntax is: a[firstIndexInclusive:endIndexExclusive:Step]
>>> a = range(20)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[7:] #seventh term and forward
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[:11] #everything before the 11th term
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[::2] # even indexed terms. 0th, 2nd, etc
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> a[4:17]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> a[4:17:2]
[4, 6, 8, 10, 12, 14, 16]
>>> a[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[19:4:-5]
[19, 14, 9]
>>> a[1:4] = [100, 200, 300] #you can assign to slices too
>>> a
[0, 100, 200, 300, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list.reverse() reverses in-place and returns nothing (None). so you don't say:
mylist = mylist.reverse()
you say:
mylist.reverse()
or alternatively:
mylist = list(reversed(mylist))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With