Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Lexicographically smallest string in mixed list

  • Last Updated : 27 Aug, 2019

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.




# 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))
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.




# 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))
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
Recommended Articles
Page :

Start Your Coding Journey Now!