I have a list of all US state names.
states = ['Oklahoma', 'Kansas', 'North Carolina', 'Georgia', 'Oregon',
'Mississippi', 'Minnesota', 'Colorado', 'Alabama',
'Massachusetts', 'Arizona', 'Connecticut', 'Montana',
'West Virginia', 'Nebraska', 'New York', 'Nevada', 'Idaho',
'New Jersey', 'Missouri', 'South Carolina', 'Pennsylvania',
'Rhode Island', 'New Mexico', 'Alaska', 'New Hampshire',
'Tennessee', 'Washington', 'Indiana', 'Hawaii', 'Kentucky',
'Virginia', 'Ohio', 'Wisconsin', 'Maryland', 'Florida',
'Utah', 'Maine', 'California', 'Vermont', 'Arkansas', 'Wyoming',
'Louisiana', 'North Dakota', 'South Dakota', 'Texas',
'Illinois', 'Iowa', 'Michigan', 'Delaware']
I want to find the longest string in this list of items, which is easy enough with the following:
def longest_state(data):
return(max(states,key=len))
print(longest_state(states)
This returns "North Carolina", which has a length of 14. However, "South Carolina" is also 14, but is not returned.
I tried to use the following stackoverflow thread which has an example to find multiple longest strings using a list comprehension but I was unable to make it work... Python's most efficient way to choose longest string in list?
I also tried using if/else statements to append the list item to another variable if it equaled the length of the current longest item but was unsuccessful
Can anyone help?