Eugene
Eugene

Reputation: 27

Get the longest string from the list using python

I tried to print "longest string" in my list. I don't want to use max() or sort(). I just tried to print it without Method. I've succeeded in getting the shortest string, but I'm having trouble printing out the longest string. Their output is the same.

a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
    a=len(i)
    b_list.append(a)
p=b_list[0]    
for k in b_list:
    if k<=p:
        k=p
        r=b_list.index(p)
        print("Shortest string : " ,a_list[r])
        break

This is the shortest string output and, of course, there is an item with a length of 3 other than 'abc' but I first entered break so that the list index only prints the smallest value.

a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
    a=len(i)
    b_list.append(a)

p=b_list[0]    
for k in b_list[1:]:
    if k>p:
        k=p
    r=b_list.index(p)
    print("longest string : " ,a_list[r])
    break

This is the result of printing the one with the longest string.What's the problem? Once again, I want to solve it without using the min(),max(),sort() method.

Upvotes: 0

Views: 782

Answers (6)

flyingbot91
flyingbot91

Reputation: 33

I think one single for loop is enough:

a = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']

longest_str_length = 0
longest_str = None

for i in a:
    curr_length = len(i)
    if curr_length > longest_str_length:
        longest_str_length = curr_length
        longest_str = i

print("Longest string is '{}'".format(longest_str))

Upvotes: 0

Swam
Swam

Reputation: 19

You could just define first element as max and compare with rest and replace the max if the new max is found.

a_list=['abc','bcd','bcdefg','abba','cddc','opq']

max = a_list[0]

for i in a_list[1:]:
    if len(i)>len(max):
        max = i
print(max)

Upvotes: 0

Artiom  Kozyrev
Artiom Kozyrev

Reputation: 3826

You can fin the longest and the shortest strings in the list the following way:

a_list = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']

# we give the both variable the value of the first element in the list 
shortest = a_list[0]  
longest = a_list[0]  

for i in a_list[1:]:
    if len(shortest) >= len(i):  # check if current string in shorter 
        shortest = i # if yes change variable value

    if len(longest) <= len(i):  # check if current string in longer
        longest = i # if yes change variable value

# print results:
print(shortest)
print(longest)

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51623

This is a very convoluted way to do it. @rdas already told you about p=k instead of k=p. You need to only loop once through your list:

a_list = ['abc','bcd','bcdefg','abba','cddc','opq']
shortest, *remaining = a_list  # 'abc' and ['bcd','bcdefg','abba','cddc','opq']
ls = len(shortest)

for word in remaining:
    if len(word) < ls:
        shortest = word
        ls = len(word)
print(shortest)

Upvotes: 0

Saurabh Kansal
Saurabh Kansal

Reputation: 702

a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
    a=len(i)
    b_list.append(a)

max_len_element  =a_list[0]
max = b_list[0]

for i in range(len(b_list)):
    if b_list[i] > max:
        max = b_list[i]
        max_len_element = a_list[i]
        print(max, max_len_element)

max_len_element is your max length element

Upvotes: 0

rdas
rdas

Reputation: 21275

You're assigning to k - the iteration variable, not p the maximum.

Also the indentaion needs to be fixed slightly:

a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
    a=len(i)
    b_list.append(a)

p=b_list[0]    
for k in b_list[1:]:
    if k>p:
        p = k
r=b_list.index(p)
print("longest string : " ,a_list[r])

Output:

longest string :  bcdefg

P.S: Your shortest string code suffers from the same issues. It just happens to work with that particular input

Upvotes: 1

Related Questions