You’ll never guess the most concise way to solve this problem… π¦
Coding Challenge
π¬ Challenge: Given a dictionary with string keys. Get the string key with the minimum number of characters, i.e., the shortest string.
A variant of this problem is to get the length of the shortest string — I’ll give you a quick and easy solution for this too. But first, let’s look at an example next, shall we?
Say, you have a dictionary with string keys:
d = {'alice': 18, 'bob': 23, 'carl': 35, 'david': 42}
Desired Output: There are two variants of this challenge.
- Get the shortest string, i.e.,
'bob'
. - Get the length of the shortest string, i.e.,
3
.
Let’s learn the most Pythonic way to solve this challenge next!
Method 1: Get the Shortest String in Dict Keys
To get the shortest string key of a dictionary, call min(dict, key=len)
. You pass the dictionary as a first argument. Python will automatically retrieve the keys from the dictionary and ignore the dictionary values. Then pass the key=len
as a second argument to compare the length of the strings and avoid the standard lexicographical string comparison.
Here’s the example:
d = {'alice': 18, 'bob': 23, 'carl': 35, 'david': 42} print(min(d, key=len)) # 'bob'
π‘ The key argument of the min()
function allows you to redefine what “minimum” means. In our example, minimum means “shortest” string and not “lexicographically shortest” string.
Without it, the min()
function on a string list would return the lexicographically minimum string, i.e., the one that comes first in the alphabet:
>>> min(['aaaaaa', 'z']) 'aaaaaa'
You can learn more about the key argument of the min()
function in our detailed blog tutorial.
π Recommended Tutorial: Get Key with Minimum Value in a Python Dictionary
Method 2: Get the Length of the Shortest String in Dict Keys
To get the length of the shortest string of a dictionary, first find the shortest string using min(dict, key=len)
and pass the resulting string in the len()
function.
Here’s the example:
d = {'alice': 18, 'bob': 23, 'carl': 35, 'david': 42} print(len(min(d, key=len))) # 3
Method 3: Get the Shortest String From Dict Values
To get the shortest string from all dictionary values, call min(dict.values(), key=len)
. You pass the dictionary values as a first argument. Then pass the key=len
as a second argument to compare the length of the strings and avoid the standard lexicographical string comparison.
Here’s the example:
d = {1: 'a', 2: 'aaa', 3:'aa'} print(min(d.values(), key=len)) # a
π Related Tutorial: How to Get the Longest String From a Python Dictionary?
Method 4: Brute-Force Pure Python
When I was first starting out, I was always looking for the solution that I understood most. I don’t recommend the following solution, it’s super long—but at least it’s simple to understand and you should only include code in your projects that you understand.
You can find the shortest string from the dictionary keys by manually comparing all strings in a for loop, keeping track of the minimum by using the len()
function and the smaller than <
operator.
d = {'aaa':1, 'aaaa':2, 'a':3} shortest = '' for s in d.keys(): if len(s) < len(shortest): shortest = s print('The shortest string is: ', shortest) # The shortest string is: a
This code snippet is probably most understandable by coders coming from other programming languages such as C++ or Java. However, a skilled Python coder would never write something like this!
π Recommended Tutorial: What is the Most Profitable Programming Language?
Programming Humor
π‘ Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.
~~~
- Question: Why do Java programmers wear glasses?
- Answer: Because they cannot C# …!
Feel free to check out our blog article with more coding jokes. π