Skip to content
Related Articles

Related Articles

Python – String with most unique characters

Improve Article
Save Article
  • Last Updated : 06 Mar, 2020
Improve Article
Save Article

Sometimes, while working with python strings, we can have a problem in which we desire to extract particular list which has most number of unique characters. This kind of problem can have application in competitive programming and web development domain. Lets discuss certain ways in which this task can be performed.

Method #1 : Using max() + dictionary comprehension
The combination of above functionalities can be used to perform this task. In this, we firstly collect the frequency of each character using dictionary and then employ max() to compute the string with most unique characters.




# Python3 code to demonstrate 
# String with most unique characters
# using max() + dictionary comprehension
  
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeksc']
  
# printing original list
print("The original list is : " + str(test_list))
  
# String with most unique characters
# using max() + dictionary comprehension
temp =  {idx : len(set(idx)) for idx in test_list}
res = max(temp, key = temp.get)
  
# printing result 
print ("The string with most unique characters is : " + str(res))

Output :

The original list is : ['gfg', 'is', 'best', 'for', 'geeksc']
The string with most unique characters is : geeksc

 

Method #2 : Using max() + key + lambda
The combination of above methods can be used to perform this task. In this, we perform this task in similar way as above, just the difference is instead of using comprehension we use lambda function to extend logic to each string for computation.




# Python3 code to demonstrate 
# String with most unique characters
# using max() + key + lambda
  
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeksc']
  
# printing original list
print("The original list is : " + str(test_list))
  
# String with most unique characters
# using max() + key + lambda
res = max(test_list, key = lambda sub: len(set(sub)), default = None)
  
# printing result 
print ("The string with most unique characters is : " + str(res))

Output :

The original list is : ['gfg', 'is', 'best', 'for', 'geeksc']
The string with most unique characters is : geeksc

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!