1

I have a list where the longest word is not a real English word, so I'd like to find the second longest word in my list to make sure it is an English word.

words is the name of my list

I've tried

longest_word = max(words, key=len)-2
longest_word = max(words, key=len)[:-2]

I've even tried

from itertools import groupby
[next(g) for _, g in groupby(sorted(longest_word, key=len), len][-2]

How do I find the second longest word in my list?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
peoplet
  • 81
  • 1
  • 7
  • 2
    hmm, have you tried `sorted(words, key=len)[-2]`? – rv.kvetch Nov 04 '21 at 03:36
  • 1
    That seems to have done it. If you wish to make your comment an answer I would mark it as the accepted answer and upvote it @rv.kvetch – peoplet Nov 04 '21 at 03:40
  • 1
    hmm, it was a good idea, unfortunately looks like it got closed. still a good thought, and appreciate it :-) – rv.kvetch Nov 04 '21 at 03:52
  • Welcome to Stack Overflow. Questions are closed as duplicates because it helps other people search the website later, and because it saves effort from other answerers. We aren't scolding you for failing to find the duplicate - we are *finding it for you* and giving you a link, i.e., answering your question. Also, please keep in mind that if you object to how your question was handled, that complaint does not belong in an edit to the question - it belongs either in a comment, or on https://meta.stackoverflow.com. – Karl Knechtel Nov 04 '21 at 04:35
  • How many points does one get for making a question Duplicate or Closed? @KarlKnechtel – peoplet Nov 04 '21 at 04:38
  • @rv.kvetch 's approach is covered by another duplicate which I have added. I found it by searching `python find 2nd longest word in list`, though I did have to scroll down a bit. – Karl Knechtel Nov 04 '21 at 04:38
  • "How many points does one get for making a question Duplicate or Closed?" Zero. I am not motivated by the reputation system, and as far as I can tell, neither are most long-term users of the site. – Karl Knechtel Nov 04 '21 at 04:39
  • 1
    Another thing that can help when searching is to generalize a bit. For example `python n longest` gives me [another candidate](https://stackoverflow.com/questions/52244850/how-to-find-the-longest-n-words-from-a-list-using-python) as the first result, though that isn't quite the same question. – Karl Knechtel Nov 04 '21 at 04:43
  • I just googled your search @KarlKnechtel I'm on the fifth page of google and am not seeing it. I think this is the okay time to ask a question. And that is interesting about the zero points. Thanks for answering. – peoplet Nov 04 '21 at 04:43
  • Note that I chose the "Get the second largest number in a list in linear time" question as the linked duplicate because it solves the question in *O(n)* time. With the accepted answer (and the other linked duplicate that someone else added), you have to spend *O(n log n)* time instead to get the list sorted. – blhsing Nov 04 '21 at 05:08
  • If you think "Get the second largest number in a list in linear time" isn't quite the same question as "Get the second longest word from a list", it's actually very much the same, since you can convert the list of words to a list of tuples of length of words and the words themselves, so their tuple order would be by word length while the words can be accessed as the second item of the tuples. – blhsing Nov 04 '21 at 05:15
  • So a new coding person tying to find the second longest word in a list will know to search for "Get the second largest number in a list in linear time"? And be able to intuitively understand "you can convert the list of words to a list of tuples of length of words and the words themselves, so their tuple order would be by word length while the words can be accessed as the second item of the tuples." ? I don't think so. In fact I know so. My question is better for this situation and more clear when searching. You are stifling that @blhsing – peoplet Nov 04 '21 at 15:44
  • The second linked question does not work for a list. It is not a good reason to call this duplicate @blhsing – peoplet Nov 04 '21 at 16:24
  • @WilhelmGleiss The second linked question was added by someone else, not me. It is still a legitimate duplicate, however, even if inefficient. If you expect every linked duplicate to be identical in every detail to your question, it is not how duplicates work on SO. – blhsing Nov 05 '21 at 06:08
  • Both of the linked duplicates are sufficiently close to your question and both solve the essence of your problem. You are supposed to figure out what trivial differences there are on your own, and if you are still stuck after applying the solution provided by the linked duplicates to your problem, you are welcome to post a new question with the new attempts you've made included. – blhsing Nov 05 '21 at 06:10
  • If you had Googled [find the second longest word in a list in python](https://www.google.com/search?q=find+the+second+longest+word+in+a+list+in+python), you would've found the second linked duplicate at the top of the search result. It is identical in every way to your question except that it needs its given sentence to be split into a list of words first. If you can't even recognize this trivial step being the only difference, you have more fundamental lessons to take first before coding. – blhsing Nov 05 '21 at 06:37

1 Answers1

1

You can try something like this

sorted(words, key=len)[-2]
Rasel
  • 50
  • 7