Skip to content
Related Articles

Related Articles

Python program to sort strings by substring range

Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

Given a String List, sort by a range of substrings.

Input : test_list = [“geeksforgeeks”, “best”, “geeks”, “preparation”, “interview”], i, j = 1, 3 
Output : [‘geeksforgeeks’, ‘geeks’, ‘best’, ‘interview’, ‘preparation’]

Explanation : “eek” < “eek” < “est” < “nte” < “rep”.

Input : test_list = [“apple”, “orange”, “banana”], i, j = 2, 4 
Output : [‘orange’, ‘banana’, ‘apple’] 

Explanation : “ang” < “nan” < “ple”. 

Method #1 : Using sort() + slicing

In this, we perform the task of sorting using sort() and the task of extracting a range is done using slicing.

Python3




# Python3 code to demonstrate working of
# Sort Strings By Substring Range
# Using sort() + slicing
 
# helper function
def get_substr(test_str):
     
    # getting range
    return test_str[i : j]
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "preparation"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
i, j = 1, 3
 
# calling func.
test_list.sort(key=get_substr)
 
# printing result
print("Strings after sorting : " + str(test_list))

Output

The original list is : [‘geeksforgeeks’, ‘best’, ‘geeks’, ‘preparation’] Strings after sorting : [‘geeksforgeeks’, ‘geeks’, ‘best’, ‘preparation’]

Time Complexity: O(nlogn)

Auxiliary Space: O(n)

Method #2 : Using lambda function + sort() + slicing

In this, we perform slicing using lambda function rather than calling external function.

Python3




# Python3 code to demonstrate working of
# Sort Strings By Substring Range
# Using lambda function + sort() + slicing
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "preparation"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
i, j = 1, 3
 
# lambda function providing sort fnc.
test_list.sort(key=lambda test_str : test_str[i : j])
 
# printing result
print("Strings after sorting : " + str(test_list))

Output

The original list is : [‘geeksforgeeks’, ‘best’, ‘geeks’, ‘preparation’] Strings after sorting : [‘geeksforgeeks’, ‘geeks’, ‘best’, ‘preparation’]

Time Complexity: O(nlogn)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!