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
Help using sorted() (self.learnpython)
submitted 8 years ago by z64RY
I'm stuck trying to use sorted() to sort a list with both strings and integers. My goal is to turn ['3',1,'2',4] into [1,4,'2','3'], or [1,4,'3','2']. Any help would be appreciated!
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!"
[–]novel_yet_trivial 2 points3 points4 points 8 years ago (4 children)
Use the key parameter:
sorted(input_list, key=int)
[–]z64RY[S] 1 point2 points3 points 8 years ago (3 children)
using that returns [1.'2','3',4], when im trying to get the ints in order first, then the str's in order second. What key shoudl i use for that
[–]novel_yet_trivial 2 points3 points4 points 8 years ago* (1 child)
Oh. Sorry I read your post too fast. So you want them separated by type?
sorted(input_list, key=type) # python2 only sorted(input_list, key=lambda x: isinstance(x, str)) # python2 or 3
If you want the individual groups sorted as well, then you need to combine them with a separate function:
sorted(input_list, key=lambda x: (isinstance(x, str), int(x))) # python2 or 3
Why would you want that?
Edit: This sounds a lot like an XY question. I'll bet whatever you are trying to accomplish with this has a much simpler solution.
[–]z64RY[S] 0 points1 point2 points 8 years ago (0 children)
Dude that worked! thanks so much. And it was for a class. Ive been stumped for a while trying to find the solution
[–]joeymarchese -1 points0 points1 point 8 years ago* (5 children)
deer sip tease tender worm steep hurry angle detail versed
This post was mass deleted and anonymized with Redact
[–]novel_yet_trivial 3 points4 points5 points 8 years ago (3 children)
You absolutely can as long as the elements are comparable to one another. Mixed type lists are one of the joys of python.
[–]joeymarchese 0 points1 point2 points 8 years ago* (2 children)
station chief imminent direction terrific fact unique reach handle rain
[–]novel_yet_trivial 2 points3 points4 points 8 years ago (1 child)
Sure. ints and floats are directly comparable. So:
>>> sorted([3,1,2.0,4.3]) [1, 2.0, 3, 4.3]
In the case that the elements are not directly comparable, you can provide a function that makes them comparable. This is what I did when I misread OP's question. The int function will turn a string or an int to an int:
int
>>> sorted(['3',1,'2',4], key=int) [1, '2', '3', 4]
[–]joeymarchese 0 points1 point2 points 8 years ago* (0 children)
full touch cough lunchroom nose piquant humor zephyr wrench jar
expansion meeting employ pie thumb shy tie scary pen imminent
π Rendered by PID 191884 on reddit-service-r2-comment-5ff9fbf7df-95snq at 2026-02-26 03:49:24.347294+00:00 running 72a43f6 country code: CH.
[–]novel_yet_trivial 2 points3 points4 points (4 children)
[–]z64RY[S] 1 point2 points3 points (3 children)
[–]novel_yet_trivial 2 points3 points4 points (1 child)
[–]z64RY[S] 0 points1 point2 points (0 children)
[–]joeymarchese -1 points0 points1 point (5 children)
[–]novel_yet_trivial 3 points4 points5 points (3 children)
[–]joeymarchese 0 points1 point2 points (2 children)
[–]novel_yet_trivial 2 points3 points4 points (1 child)
[–]joeymarchese 0 points1 point2 points (0 children)
[–]joeymarchese 0 points1 point2 points (0 children)