0

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'))
5
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by pyguy62
0

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

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.

0

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 by pyguy62: n/a

0

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

yes

1

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.

0

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.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.