Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Find The Longest And Shortest Strings And Lengths In A Python List

You can the shortest and longest items in a Python list of strings by using [TODO: Code shorthand span ] with the built - in [TODO: Code shorthand span ] and [TODO: Code shorthand span ] functions.

For example :

python
state_names = ['Alabama', 'California', 'Iowa', 'Pennsylvania' ]

shortest_name = min(state_names, key=len)
longest_name = max(state_names, key=len)

print(shortest_name)
print(longest_name)

Outputs :

Iowa
Pennsylvania

A useful addition is to call [TODO: Code shorthand span ] on the results to find the lengths.

For example :

python
state_names = ['Alabama', 'California', 'Iowa', 'Pennsylvania' ]

shortest_string = len(min(state_names, key=len))
longest_string = len(max(state_names, key=len))

print(shortest_string)
print(longest_string)

Outputs :

4
12

Short and sweet.

Happy coding.