Why does this return None?

list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished']
print list1.sort(key=lambda s : s.count('a'))

The method sort() sorts the list in place and returns None

>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished']
>>> list1.sort(key=lambda s : s.count('a'))
>>> print list1
['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant']

The function sorted() creates a new sorted list and does not modify the initial list

>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished']
>>> print sorted(list1, key=lambda s : s.count('a'))
['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant']
>>> print list1
['Herring', 'used', 'to', 'be', 'abundant', 'in', 'the', 'AtlAntic', 'Ocean', 'then', 'herring', 'got', 'overfished']

sort is in place function, sometimes in other languages called procedure, which does not return value, but directly orders the list. So you must print list1 next line after sort or you should use sorted function.

The method sort() sorts the list in place and returns None

>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished']
>>> list1.sort(key=lambda s : s.count('a'))
>>> print list1
['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant']

The function sorted() creates a new sorted list and does not modify the initial list

>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished']
>>> print sorted(list1, key=lambda s : s.count('a'))
['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant']
>>> print list1
['Herring', 'used', 'to', 'be', 'abundant', 'in', 'the', 'AtlAntic', 'Ocean', 'then', 'herring', 'got', 'overfished']

is sorted(list1, key=lambda s : s.count('a')) sorting the list by the amount of lower case a's in each string?

Edited 5 Years Ago by pyguy62: n/a

is sorted(list1, key=lambda s : s.count('a')) sorting the list by the amount of lower case a's in each string?

yes

pardon me, I mis-phrased the question. I meant, why is it sorted in reverse.

Not really reverse - lowest to highest makes most sense,
1,2,3,4,5
instead of
5,4,3,2,1
is the way most people count ;)

However, if you do want it highest from lowest, then simply past a keyword argument "reverse=True", which will work for both sort and sorted.

Not really reverse - lowest to highest makes most sense,
1,2,3,4,5
instead of
5,4,3,2,1
is the way most people count ;)

However, if you do want it highest from lowest, then simply past a keyword argument "reverse=True", which will work for both sort and sorted.

lol I knew how to count and about the reverse option ;) I was thinking of it more as prioritizing, but the count makes sense. Thanks E.

This question has already been answered. Start a new discussion instead.