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']

Thank you!

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?

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

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

yes

Member Avatar for Enalicho

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.