- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find k longest words in given list in Python
We have a scenario where we have to pick the top n longest word from a list containing many words of varying length. In this article we will see various approaches to achieve that.
With count() and sorted()
We first sort the elements of the list in the reverse order so that the longest words are available at the beginning of the list. Then find the length of each word and add the result of the count to a variable. Finally take a slice of the required number of longest words we need.
Example
from itertools import count def longwords(l, x): c = count() return sorted(l, key=lambda i: (len(i), next(c)), reverse=True)[:x] listA = ['Earth','Moonshine','Aurora','Snowflakes','Sunshine'] n = 2 print(longwords(listA, n))
Output
Running the above code gives us the following result −
['Snowflakes', 'Moonshine']
With zip and enumerate
In this approach we use enumerate to list out each element of the list and then apply sorted and zip function to get the count. The negative length values indicate the reverse order of sorting and finally we slice the required number of counts.
Example
def longwords(l, x): idx, words = zip(*sorted(enumerate(l), key = lambda i: (-len(i[1]), -i[0]))[:x]) return list(words) listA = ['Earth','Moonshine','Aurora','Snowflakes','Sunshine'] n = 2 print(longwords(listA, n))
Output
Running the above code gives us the following result −
['Snowflakes', 'Moonshine']
- Related Articles
- Find the longest substring with k unique characters in a given string in Python
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Program to find length of longest alternating subsequence from a given list in Python
- Program to find length of longest arithmetic subsequence of a given list in Python
- How to Find the Longest Words in a Text File using Python?
- Program to find longest equivalent sublist after K increments in Python
- Find the k most frequent words from data set in Python
- Python - Check if given words appear together in a list of sentence
- Python - Find words greater than given length
- Find minimum k records from tuple list in Python
- Program to find length of longest contiguous sublist with same first letter words in Python
- Python Program to return the Length of the Longest Word from the List of Words
- Program to find longest common prefix from list of strings in Python
- Find the longest subsequence of an array having LCM at most K in Python
- Program to find length of longest substring which contains k distinct characters in Python
