-3

I want to print the shortest strings in a list. for example the output for:

names=["Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo"]

will be

"Ed", "Jo"

The output for

names=["Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo", "Al"]

will be:

"Ed", "Jo", "Al"


[x for x in names if x==min(names, key=len)]

will get only one of them, but will not help in cases where there is more than one.

Dan Eran
  • 159
  • 1
  • 1
  • 8

5 Answers5

1

First find the length of the shortest string:

shortest = min(map(len, names))

Now just find all strings with that length:

print([name for name in names if len(name) == shortest])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

Pass len as key to the min method and get the length of the smallest string and then use list comprehension as:

names=["Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo"]
smallest_len = len(min(names, key=len))
smallest = [name for name in names if len(name) == smallest_len]
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0
names = ["Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo"]
smallest_len = len(min(names, key=len))
shorts = []
for name in names:
    if len(name) == smallest_len:
        shorts.append(name)
print(shorts)
TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • @Tomerikoo oh thanks, it took me over a minute to figure out why I am "comparing a string to an int" ! – TheEagle Mar 15 '21 at 12:41
0

Based on the question, my guess is that you're a Python beginner, so I really don't want to introduce you to advanced pythonic concepts.

Using no advanced concepts of the language, it's straight forward:

names = ["Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo"]


def get_all_shortest_strings(strings: list) -> list:  # Accept a list of strings and return a list of strings as well
    shortest_strings = []               # Start with an empty list. This is the identity element for list addition
    shortest_length = 9999999999999999  # Start with a huge number. This is the identity element for the minimum() function
    for string in strings:              # Go through all strings.
        if len(string) == shortest_length:    # Found one of the same length? 
            shortest_strings.append(string)   # Add it!
        if len(string) < shortest_length:     # Found a shorter one?
            shortest_strings = []             # Clear all longer ones
            shortest_strings.append(string)   # Add the one we just found
            shortest_length = len(string)     # remember the length
    return shortest_strings


assert ["Ed", "Jo"] == get_all_shortest_strings(names)

You may want to read about the identity element. Basically it says that this element will not change the result of a given operation.

You may also not have seen assert before. You can use asserts for checking if your code works, without the need of printing something.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
-1

To loop only once, and call len on each item once:

names = [
    "Hans", "Anna", "Vladimir", "Michael", "Ed", "Susan", "Janice", "Jo", "Al"
]


def find_shortests(names: list):
    result = []
    shortest = float("inf")
    for x in names:
        n = len(x)
        if n > shortest:
            continue
        if n < shortest:
            result.clear()
        shortest = n
        result.append(x)
    return result


print(find_shortests(names))
# ['Ed', 'Jo', 'Al']
David Pi
  • 142
  • 1
  • 2