-2

in my list I want the function to return only the maximum values within the list

my_list = ["and", "the", "plus", "from", "i" , "09"]

How can I get the output to be the maximum values ("plus", "from") I did something like that, and I could not continue ...

my_list = ["and", "the", "plus", "from", "i" , "09"]
list1 = str(my_list)
list2 = list1.split(',')

for i in list2:
    if len(i) == max(len(i)):
        print(i)

Thanks for the help

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    Does this answer your question? [Python's most efficient way to choose longest string in list?](https://stackoverflow.com/questions/873327/pythons-most-efficient-way-to-choose-longest-string-in-list) – Jerrybibo Mar 23 '21 at 13:44
  • Why do you stringify the list and then split it? – khelwood Mar 23 '21 at 13:47
  • Can you clarify what you are trying to do? Your description does not match what the code does *at all*. Why do you construct ``list2`` from the *string representation* of ``my_list`` (meaning that it has items like ``'["and"'``)? What do you think ``len(i) == max(len(i))`` does? – MisterMiyagi Mar 23 '21 at 13:49

5 Answers5

0
def myFunction(words: list):
    output = []
    for word in sorted(words, key=len, reverse=True):
        maxLen = len(sorted(words, key=len, reverse=True)[0])
        if(len(word) < maxLen):
            break
        output.append(word)
    return {
        "words": output,
        "length": maxLen
    }

It takes the words list and sorts it from greatest to least length, then sets the maxLen variable to the length of the first thing in that list. Then, it looks to see if the length of the word the for loop is less than maxLen, and if it is, it stops the loop.

0

If you would like to get the lenght of longest string in list, you can use the shorhand version foor lop like max([len(item) for item in my_list]). but as far as I understood you would like to get the the string even if there are strings with same length you can try this:

def longest_items(my_list):
    max_len = 0
    items = []
    for item in my_list:
        if len(item) > max_len:
            max_len = len(item)
            items.clear()
            items.append(item)
        elif len(item) == max_len:
            items.append(item)
    return tuple(items)

This function will return longest string or strings in a tuple.

berkeebal
  • 463
  • 4
  • 8
0

You can quickly use some list comprehension:

>>> my_list = ["and", "the", "plus", "from", "i" , "09"]
>>> lengths = [len(s) for s in my_list]
>>> max_lengths = [s for s in my_list if len(s) == max(lengths)]
>>> max_lengths
["plus", "from"]

The list in lengths stores the length of each string in my_list. Then, by using the max() function, we can quickly obtaining the maximum value in lengths, which corresponds to the maximum length of a string. Finally, we re-iterate over my_list and compare each string value to max(lengths), checking if each string is the maximum length.

Note that, if desired, this all can be condensed into one list comprehension:

>>> max_lengths = [i for i in my_list if len(i) == max([len(s) for s in my_list])]
>>> max_lengths
["plus", "from"]
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
0
    my_list = ["and", "the", "plus", "from", "i", "09"]
    
    length = 0
    arr = []
    for i in my_list:
        if len(i) > length:
            length = len(i)
            arr = []
        if len(i) == length:
            arr.append(i)
            length = len(i)
    
    print(arr)
-2
list2 = list1.split(',')

split is a string function, it turns a string into a list. Like "john, tim, and paul" would turn into ["john", " tim", " and paul"]

max takes an iterator and returns the highest value. you did max(len(i)) ... len(i) is just going to return an integer, so you did max(6). Instead, you want to make a list of the lengths. max([len(x) for x in list1]) would give you the length of the longest item in list1.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Paul Becotte
  • 9,767
  • 3
  • 34
  • 42