Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list.sort() returning None
#1
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())
Reply
#2
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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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!
Reply
#5
@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)
Reply
#6
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.
Reply
#7
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Just joking, no harm meant.

This link shows the quote.

On the first page, down a little.
Reply
#9
@deanhystad

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 1 503 Oct-27-2023, 03:03 PM
Last Post: buran
  returning a List of Lists nafshar 3 1,078 Oct-28-2022, 06:28 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,150 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 2,899 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort List of Lists by Column Nju 1 11,591 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  How to sort os.walk list? Denial 6 11,583 Oct-10-2020, 05:28 AM
Last Post: Denial
  2d List not returning DariusKsm 2 1,714 Sep-22-2020, 05:11 PM
Last Post: DariusKsm
  Need help returning min() value of list? edwdas 3 2,093 Nov-10-2019, 09:43 PM
Last Post: snippsat
  Converting to a list and sort tantony 6 3,244 Oct-07-2019, 03:30 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020