Python | Lexicographically smallest string in mixed list

Sometimes, we can also encounter a problem in which we are given a mixed list and require to find the lexicographically smallest string that occur in list. This problem can find it’s application day-day programming. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using min() + isinstance() + list comprehension
This task can be performed using the combination of above functions. In this, the min() functions performs the task of finding smallest string and isinstance() is used to check for string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Lexicographically smallest string in mixed list
# Using min() + isinstance() + list comprehension
  
# initializing list
test_list = [1, 2, 4, "GFG", 5, "IS", 7, "BEST"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Lexicographically smallest string in mixed list
# Using min() + isinstance() + list comprehension
res = min(sub for sub in test_list if isinstance(sub, str))
  
# printing result 
print("The Lexicographically smallest string is : " + str(res))

chevron_right


Output :



The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST

 

Method #2 : Using min() + lambda + filter()
The combination of above functions can also be used to perform this particular task. In this we perform the task of list comprehension using filter() and lambda and min function is used to performed the usual task of finding smallest string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Lexicographically smallest string in mixed list
# Using min() + lambda + filter()
  
# initializing list
test_list = [1, 2, 4, "GFG", 5, "IS", 7, "BEST"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Lexicographically smallest string in mixed list
# Using min() + lambda + filter()
res = min(filter(lambda s:isinstance(s, str), test_list))
  
# printing result 
print("The Lexicographically smallest string is : " + str(res))

chevron_right


Output :

The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.