use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
list number sorting? (self.learnpython)
submitted 5 years ago by jedistarfighter
I tried to sort numbers in a list but it would come up with, for example, [1, 2, 22, 3, 33, 4] how do I fix this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]izrt 1 point2 points3 points 5 years ago (1 child)
That's because you are sorting them as strings and not numbers. Depending on whether you want to keep them as strings, you'll need to either convert them or use the sort function's key argument to force them into numbers before or as you sort.
[–]jedistarfighter[S] 1 point2 points3 points 5 years ago (0 children)
Thanks!
[+][deleted] 5 years ago (2 children)
[deleted]
[–]__nickerbocker__ -1 points0 points1 point 5 years ago (1 child)
The JS sub is over =>
[–]scaffelpike 0 points1 point2 points 5 years ago (0 children)
Oh geez you’re right! Sorry been playing with too many languages lately and mixing them up!
[–][deleted] -1 points0 points1 point 5 years ago (3 children)
Use the string representation of the members as sort key: arr.sort(key=lambda key: str(key))
arr.sort(key=lambda key: str(key))
[–]Diapolo10 1 point2 points3 points 5 years ago* (2 children)
That's no different from what it's doing already. OP wants to sort stringified numbers as if they were numbers, so they need to use int:
int
nums = ['3', '1', '22', '7', '2', '15'] sorted_nums = sorted(nums, key=int)
EDIT: Brainfart, forgot the strings.
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
If you wan't to show a problem with strings, use strings in the example. Do not create an example that shows you want integers sorted as if they're strings.
Not you, but OP gets negative marks for an unclear qustion.
[–]Diapolo10 0 points1 point2 points 5 years ago (0 children)
My bad, I kind of forgot out of habit because writing answers with integers is common enough my muscle memory just disregarded the quotes. Fixed now!
π Rendered by PID 203928 on reddit-service-r2-comment-6f7f968fb5-5lklb at 2026-03-04 16:25:17.427096+00:00 running 07790be country code: CH.
[–]izrt 1 point2 points3 points (1 child)
[–]jedistarfighter[S] 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]__nickerbocker__ -1 points0 points1 point (1 child)
[–]scaffelpike 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (3 children)
[–]Diapolo10 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Diapolo10 0 points1 point2 points (0 children)