Python – Extract Sorted Strings
Last Updated : 25 Apr, 2023
Given a String List, extract all sorted strings.
Input : test_list = ["hint", "geeks", "fins", "Gfg"] Output : ['hint', 'fins', 'Gfg'] Explanation : Strings in increasing order of characters are extracted.
Input : test_list = ["hint", "geeks", "Gfg"] Output : ['hint', 'Gfg'] Explanation : Strings in increasing order of characters are extracted.
Method #1 : Using list comprehension + sorted()
In this, we perform the task of sorting strings and comparison using sorted(), list comprehension is used to iterate through Strings.
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ] print ( "The original list is : " + str (test_list)) res = [sub for sub in test_list if ''.join( sorted (sub)) = = sub] print ( "Sorted Strings : " + str (res)) |
Output The original list is : ['hint', 'geeks', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(nlogn + n * klogk), where n is the number of strings in the list and k is the maximum length of a string in the list.
Auxiliary space: O(n), as we are creating a new list with the sorted strings.
Method #2 : Using filter() + lambda + sorted() + join()
In this, we perform filtering using filter() + lambda, and join() is used to convert the final sorted character list result to a string for comparison.
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ] print ( "The original list is : " + str (test_list)) res = list ( filter ( lambda sub: ''.join( sorted (sub)) = = sub, test_list)) print ( "Sorted Strings : " + str (res)) |
Output The original list is : ['hint', 'geeks', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend()+sort() methods
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ] print ( "The original list is : " + str (test_list)) res = [] for i in test_list: x = list (i) y = [] y.extend(x) y.sort() if (x = = y): res.append(i) print ( "Sorted Strings : " + str (res)) |
Output The original list is : ['hint', 'geeks', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using a for loop and a separate list for storing sorted strings
- Initialize an empty list to store the sorted strings.
- Loop through each string in the input list.
- For each string, use the sorted() function to sort the characters in the string in ascending order and then join them back into a string.
- Compare the sorted string obtained in step 3 with the original string.
- If the sorted string is equal to the original string, it means that the original string contains characters in sorted order, so append it to the list of sorted strings.
- After processing all strings in the input list, return the list of sorted strings.
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ] print ( "The original list is : " + str (test_list)) res = [] for string in test_list: if ''.join( sorted (string)) = = string: res.append(string) print ( "Sorted Strings : " + str (res)) |
Output The original list is : ['hint', 'geeks', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(n * klogk), where n is the length of the input list and k is the maximum length of a string in the list, due to the sorting operation.
Auxiliary space: O(n), for storing the output list of sorted strings.
Method #5: Using a generator expression with sorted() and if condition
In this method, we can use a generator expression with sorted() function and an if condition to check if the sorted string is equal to the original string. If it is, we can yield the original string.
Approach:
- Use a generator expression with sorted() function to check if the sorted string is equal to the original string.
- Yield the original string if it is sorted.
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ] print ( "The original list is : " + str (test_list)) res = (string for string in test_list if ''.join( sorted (string)) = = string) print ( "Sorted Strings : " + str ( list (res))) |
Output The original list is : ['hint', 'geeks', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(n * k log k), where n is the length of the list and k is the length of the longest string in the list.
Auxiliary space: O(n), where n is the length of the list.
Similar Reads
Python | Extract K sized strings
Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific sized strings. This kind of problem can occur during validation cases across many domains. Let's discuss certain ways to handle this in Python strings list. Method #1 : Using list compr
5 min read
Python - Extract range sized strings
Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific range sized strings. This kind of problem can occur during validation cases across many domains. Letâs discuss certain ways to handle this in Python strings list. Method #1 : Using list
4 min read
Python | Extract Score list of String
Sometimes, while programming we can have a problem in which we dedicate to each character of alphabets a particular score and then according to string, extract only those score for further computations. This can have application in gaming domain. Let's discuss certain ways in which this task can be
3 min read
Python Extract Substring Using Regex
Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a
2 min read
Python - Extract K length substrings
The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in Python Using List Comprehension List comprehension is
3 min read
Python | Sort each String in String list
Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Usi
4 min read
Python Program to Sort a String
Sorting strings in Python is a common and important task, whether we need to organize letters alphabetically or systematically handle text data. In this article, we will explore different methods to sort a string starting from the most efficient to the least. Using sorted with join()sorted() functio
2 min read
Reverse Sort a String - Python
The goal is to take a given string and arrange its characters in descending order based on their Unicode values. For example, in the string "geeksforgeeks", the characters will be sorted from highest to lowest, resulting in a new string like "ssrokkggfeeeee". Let's understand different methods to pe
2 min read
Python | K Character Split String
The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Letâs discuss certain
4 min read
Sort Numeric Strings in a List - Python
We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30.
2 min read