# Python – Sort String list by K character frequency

Given String list, perform sort operation on basis of frequency of particular character.

Input : test_list = [“geekforgeekss”, “is”, “bessst”, “for”, “geeks”], K = ‘s’
Output : [‘bessst’, ‘geekforgeekss’, ‘geeks’, ‘is’, ‘for’]
Explanation : bessst has 3 occurrence, geeksforgeekss has 3, and so on.

Input : test_list = [“geekforgeekss”, “is”, “bessst”], K = ‘e’
Output : [“geekforgeekss”, “bessst”, “is”]
Explanation : Ordered decreasing order of ‘e’ count.

Method #1 : Using sorted() + count() + lambda

In this, sorted() is used to perform task of sort, count() is as function upon which sorting is to be performed. using additional key param, and function encapsulation used is lambda.

## Python3

 `# Python3 code to demonstrate working of ` `# Sort String list by K character frequency` `# Using sorted() + count() + lambda`   `# initializing list` `test_list ``=` `[``"geekforgeeks"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K ` `K ``=` `'e'`   `# "-" sign used to reverse sort` `res ``=` `sorted``(test_list, key ``=` `lambda` `ele: ``-``ele.count(K))`   `# printing results` `print``(``"Sorted String : "` `+` `str``(res))`

Output

```The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']```

Time Complexity: O(nlogn), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

Method #2 : Using sort() + count() + lambda

In this, we perform task of sort using sort(), this is similar to above, only difference being that sorting is done inplace.

## Python3

 `# Python3 code to demonstrate working of ` `# Sort String list by K character frequency` `# Using sort() + count() + lambda`   `# initializing list` `test_list ``=` `[``"geekforgeeks"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K ` `K ``=` `'e'`   `# "-" sign used to reverse sort` `# inplace sort` `test_list.sort(key ``=` `lambda` `ele: ``-``ele.count(K))`   `# printing results` `print``(``"Sorted String : "` `+` `str``(test_list))`

Output

```The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using operator.countOf() method

## Python3

 `# Python3 code to demonstrate working of ` `# Sort String list by K character frequency` `# Using operator.countOf() ` `import` `operator as op` `# initializing list` `test_list ``=` `[``"geekforgeeks"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K ` `K ``=` `'e'`   `# "-" sign used to reverse sort` `res ``=` `sorted``(test_list, key ``=` `lambda` `ele: ``-``op.countOf(ele,K))`   `# printing results` `print``(``"Sorted String : "` `+` `str``(res))`

Output

```The original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Sorted String : ['geekforgeeks', 'geeks', 'best', 'is', 'for']```

Time Complexity: O(NLogN)
Auxiliary Space: O(N)

Method 4: Using heapq.nlargest() and count()

Step-by-step approach:

• Use the nlargest() function from the heapq module to return the n largest elements from a list of strings, sorted in descending order by the count of the target character K in each string. Set n to the length of the test_list to return all elements.
• Define a lambda function that takes a string as input and returns the count of the target character K in that string.
• Use the count() method to count the number of occurrences of K in each string in the test_list.
• Use the sorted() function to sort the test_list based on the count of K in each string. The key parameter of sorted() should be the lambda function defined in step 2.
• Return the sorted list of strings.

## Python3

 `import` `heapq`   `# initializing list` `test_list ``=` `[``"geekforgeeks"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``]`   `# initializing K ` `K ``=` `'e'`   `# define lambda function to count occurrences of K in a string` `count_K ``=` `lambda` `s: s.count(K)`   `# use nlargest to sort test_list based on count of K in each string` `n ``=` `len``(test_list)` `sorted_list ``=` `heapq.nlargest(n, test_list, key``=``count_K)`   `# use sorted to sort test_list based on count of K in each string` `sorted_list ``=` `sorted``(test_list, key``=``count_K, reverse``=``True``)`   `# print results` `print``(``"Sorted String: "``, sorted_list)`

Output

`Sorted String:  ['geekforgeeks', 'geeks', 'best', 'is', 'for']`

Time complexity: O(n*log(n)) for sorting the list of strings.
Auxiliary space: O(n) for storing the list of strings.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next