Python Forum

Full Version: list.sort() returning None
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

So I'm trying to code a script that uses requests and bs4 to go to https://news.ycombinator.com/news, and print out the most number of upvotes. I have a list of all the upvotes, but when a call print(articles_upvotes.sort()) it just prints None. Some thing happens if I save it to a variable and print the new variable. Here's the code:

from bs4 import BeautifulSoup
import requests

yc_web_page = requests.get("https://news.ycombinator.com/news").text
yc_soup = BeautifulSoup(yc_web_page, "html.parser")
articles = yc_soup.find_all(name="tr", class_="athing")

articles_text = []
articles_links = []
articles_upvotes = [score.getText().split()[0] for score in yc_soup.find_all(name="td", class_="subtext")]
articles_upvotes = [int(score) for score in articles_upvotes]

for article_tag in articles:
    article_text = article_tag.getText()
    article_link = article_tag.select("a")[0].get("href")
#    article_upvote = yc_soup.find_all(name="td", class_="subtext")[0].select("span.score")[0].text.split()[0]
#    print(article_upvote)

    articles_text.append(article_text)
    articles_links.append(articles_links)
#    articles_upvotes.append(article_upvote)

print(articles_text)
print(articles_links)
print(articles_upvotes.sort())
Reading documentation is always a good idea:

Quote: list.sort(*, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
There is no mention in the Documentation, that this method return a None.
But, it returns None and sorts the list in-place.

I guess the expectation comes from str. The str methods always return a new str, because str is immutable.

If the original order is also required, then use sorted. If you don't care about the previous order, just use the sort method of the list instance. But do not assign the result to the name. Then the name refers to None and the list instance disappears after the garbage collector has done its work.
Smile Smile Smile

Dead_EyE: a very accurate shooter (and reader?)

Quote:You can also use the list.sort() method. It modifies the list in-place (and returns None to avoid confusion).

Big Grin Big Grin Big Grin

Seems, the "avoid confusion" part is not wholly true!
@DeaD_EyE I think I did what you meant. I ran

highest_upvote = articles_upvotes.sort()
print(highest_upvote)
But that still returns None. Is that what you meant? If not, could you include maybe a code example? I just starting coding in Python last year, so I don't know what a lot of things mean (eg. The garbage collector? What's that? Also, I'm only a home schooled teenager so I don't know a lot of things in general)
In this code:
print(articles_upvotes.sort())
sort changes the order of objects in articles_upvotes and returns None. The print command does not print articles_upvotes, it prints the value returned by articles_upvotes.sort().

You can fix this by printing articles_upvotes like this:
articles_upvotes.sort()
print(articles_upvotes)
Or you can use sorted that returns a sorted copy of articles_upvotes. articles_upvotes is unchanged.
print(sorted(articles_upvotes))
All functions return a value. Often the returned value is None. You should read the documentation and look to see what value is returned. If the return value isn't mentioned, it is probably None.
(Mar-18-2024, 10:39 AM)Pedroski55 Wrote: [ -> ]Smile Smile Smile

Dead_EyE: a very accurate shooter (and reader?)

Quote:You can also use the list.sort() method. It modifies the list in-place (and returns None to avoid confusion).

Big Grin Big Grin Big Grin

Seems, the "avoid confusion" part is not wholly true!

Explicit is better than implicit.

I've forgotten to post the link to the documentation: https://docs.python.org/3/library/stdtyp...#list.sort
But there I can't find the hint, that this method does return None.
Just joking, no harm meant.

This link shows the quote.

On the first page, down a little.
@deanhystad

This code worked. Thanks. I am so dumb some times