Python: Finding Longest/Shortest Words In a List and Calling Them in a Function












3















I have a list of words:
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
I have two functions that are supposed to find the shortest and longest words in this list:



def bigWords(list=, *args):
largestWord=""
largestLen=0
for word in list:
if largestWord<len(word):
largestWord=len(word)
largestWord=word
print "The longest word(s) in the list is %s." % largestWord

def smallWords(list=, *args):
smallestWord=""
smallestLen=0
for word in list:
if smallestLen>len(word):
smallestLen>len(word)
smallestWord=word
print "The shortest word(s) in the list is: %s." % (smallestWord)


I have these functions nested so I can call them all at once:



def callFunctions():
###Words###
words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

wordLength=lenList(words)
print "The amount of words is %d" % wordLength
func_list2 = [bigWords, smallWords]
for f in func_list2:
map(f, words)

callFunctions()


This is just returning this without inputing the words in the list:



The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The longest word(s) in the list is .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .
The shortest word(s) in the list is: .


Not sure why, any help is appreciated.










share|improve this question



























    3















    I have a list of words:
    words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
    I have two functions that are supposed to find the shortest and longest words in this list:



    def bigWords(list=, *args):
    largestWord=""
    largestLen=0
    for word in list:
    if largestWord<len(word):
    largestWord=len(word)
    largestWord=word
    print "The longest word(s) in the list is %s." % largestWord

    def smallWords(list=, *args):
    smallestWord=""
    smallestLen=0
    for word in list:
    if smallestLen>len(word):
    smallestLen>len(word)
    smallestWord=word
    print "The shortest word(s) in the list is: %s." % (smallestWord)


    I have these functions nested so I can call them all at once:



    def callFunctions():
    ###Words###
    words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

    wordLength=lenList(words)
    print "The amount of words is %d" % wordLength
    func_list2 = [bigWords, smallWords]
    for f in func_list2:
    map(f, words)

    callFunctions()


    This is just returning this without inputing the words in the list:



    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The longest word(s) in the list is .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .
    The shortest word(s) in the list is: .


    Not sure why, any help is appreciated.










    share|improve this question

























      3












      3








      3


      3






      I have a list of words:
      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
      I have two functions that are supposed to find the shortest and longest words in this list:



      def bigWords(list=, *args):
      largestWord=""
      largestLen=0
      for word in list:
      if largestWord<len(word):
      largestWord=len(word)
      largestWord=word
      print "The longest word(s) in the list is %s." % largestWord

      def smallWords(list=, *args):
      smallestWord=""
      smallestLen=0
      for word in list:
      if smallestLen>len(word):
      smallestLen>len(word)
      smallestWord=word
      print "The shortest word(s) in the list is: %s." % (smallestWord)


      I have these functions nested so I can call them all at once:



      def callFunctions():
      ###Words###
      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

      wordLength=lenList(words)
      print "The amount of words is %d" % wordLength
      func_list2 = [bigWords, smallWords]
      for f in func_list2:
      map(f, words)

      callFunctions()


      This is just returning this without inputing the words in the list:



      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .


      Not sure why, any help is appreciated.










      share|improve this question














      I have a list of words:
      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
      I have two functions that are supposed to find the shortest and longest words in this list:



      def bigWords(list=, *args):
      largestWord=""
      largestLen=0
      for word in list:
      if largestWord<len(word):
      largestWord=len(word)
      largestWord=word
      print "The longest word(s) in the list is %s." % largestWord

      def smallWords(list=, *args):
      smallestWord=""
      smallestLen=0
      for word in list:
      if smallestLen>len(word):
      smallestLen>len(word)
      smallestWord=word
      print "The shortest word(s) in the list is: %s." % (smallestWord)


      I have these functions nested so I can call them all at once:



      def callFunctions():
      ###Words###
      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

      wordLength=lenList(words)
      print "The amount of words is %d" % wordLength
      func_list2 = [bigWords, smallWords]
      for f in func_list2:
      map(f, words)

      callFunctions()


      This is just returning this without inputing the words in the list:



      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The longest word(s) in the list is .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .
      The shortest word(s) in the list is: .


      Not sure why, any help is appreciated.







      python function loops for-loop maps






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 1 '14 at 1:30









      Shayd3Shayd3

      611110




      611110
























          6 Answers
          6






          active

          oldest

          votes


















          12














          If you like, there are simpler ways to approach the problem:



          words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
          sortedwords = sorted(words, key=len)
          print "The number of words in the list is: %s." % (len(words),)
          print "The shortest word in the list is: %s." % (sortedwords[0],)
          print "The longest word in the list is: %s." % (sortedwords[-1],)


          This produces:



          The number of words in the list is: 10.
          The shortest word in the list is: up.
          The longest word in the list is: purple.





          share|improve this answer































            2















            Just use max and min functions having key as the length




                y=
            for names in range(4):
            name=raw_input('Enter:')
            y+=name,
            s=max(y,key=len)
            r=min(y,key=len)





            share|improve this answer































              1














              You are so close - but I think the problem is incallFunctions(). You are mapping the functions in func_list2 to every string in the words array, not applying the function to the array as a whole. It was a good idea to use map, which is a powerful function, but you don't need to use it here. Here is code that I tested with a simple online interpreter. Try it. Good luck with whatever you are learning/ the project you are making!



              def bigWords(list=, *args):
              largestWord=""
              for word in list:
              if len(largestWord)<len(word):
              largestWord= word
              print "The longest word(s) in the list is %s." % largestWord
              return largestWord

              def smallWords(list=, *args):
              smallestWord = bigWords(list)
              for word in list:
              if len(smallestWord)> len(word):
              smallestWord = word
              print "The shortest word(s) in the list is: %s." % (smallestWord)


              def callFunctions():
              ###Words###
              words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

              wordLength=len(words)
              print "The amount of words is %d" % wordLength
              func_list2 = [bigWords, smallWords]
              for f in func_list2:
              f(words)

              callFunctions()





              share|improve this answer































                0














                First, you have an error in the code of function bigWords. You should compare with the length and not the word as follows



                def bigWords(list=, *args):
                largestWord=""
                largestLen=0
                for word in list:
                if largestLen<len(word):
                largestLen=len(word)
                largestWord=word
                print "The longest word(s) in the list is %s." % largestWord


                Second, to use the two functions, you need to call them for the list of words and not for each word:



                def callFunctions():
                ###Words###
                words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                wordLength=lenList(words)
                print "The amount of words is %d" % wordLength
                func_list2 = [bigWords, smallWords]
                for f in func_list2:
                f(words)

                callFunctions()





                share|improve this answer
























                • How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                  – Shayd3
                  Oct 1 '14 at 1:56











                • @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                  – wasserfeder
                  Oct 1 '14 at 2:06



















                0














                Try this simple solution:



                def find_longest_and_shortest_word(list_of_words):
                longest_word = list_of_words[0]
                shortest_word = list_of_words[0]
                for word in list_of_words:
                if len(longest_word) < len(word):
                longest_word = word
                if len(shortest_word) > len(word):
                shortest_word = word
                print(f'The longest word is: {longest_word}')
                print(f'The shortest word is: {shortest_word}')
                return longest_word, shortest_word





                share|improve this answer































                  0














                  you can sort you list using sorted() function that allows you to sort the list by the length of the strings in it.for that you need to add key=len so the function will sort by length and not by alphabecit order. you also need to give to the function reverse=true
                  so it will be easier to access to the longest string (it will be in [0] at the list)



                  def longest(my_list):
                  my_list = sorted(my_list, key=len, reverse=True)
                  return my_list[0]


                  list1 = ['aaa', 'bbbb', 'cccccc', 'd']
                  print(longest(list1))






                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26132770%2fpython-finding-longest-shortest-words-in-a-list-and-calling-them-in-a-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    12














                    If you like, there are simpler ways to approach the problem:



                    words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
                    sortedwords = sorted(words, key=len)
                    print "The number of words in the list is: %s." % (len(words),)
                    print "The shortest word in the list is: %s." % (sortedwords[0],)
                    print "The longest word in the list is: %s." % (sortedwords[-1],)


                    This produces:



                    The number of words in the list is: 10.
                    The shortest word in the list is: up.
                    The longest word in the list is: purple.





                    share|improve this answer




























                      12














                      If you like, there are simpler ways to approach the problem:



                      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
                      sortedwords = sorted(words, key=len)
                      print "The number of words in the list is: %s." % (len(words),)
                      print "The shortest word in the list is: %s." % (sortedwords[0],)
                      print "The longest word in the list is: %s." % (sortedwords[-1],)


                      This produces:



                      The number of words in the list is: 10.
                      The shortest word in the list is: up.
                      The longest word in the list is: purple.





                      share|improve this answer


























                        12












                        12








                        12







                        If you like, there are simpler ways to approach the problem:



                        words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
                        sortedwords = sorted(words, key=len)
                        print "The number of words in the list is: %s." % (len(words),)
                        print "The shortest word in the list is: %s." % (sortedwords[0],)
                        print "The longest word in the list is: %s." % (sortedwords[-1],)


                        This produces:



                        The number of words in the list is: 10.
                        The shortest word in the list is: up.
                        The longest word in the list is: purple.





                        share|improve this answer













                        If you like, there are simpler ways to approach the problem:



                        words=["alpha","omega","up","down","over","under","purple","red","blue","green"]
                        sortedwords = sorted(words, key=len)
                        print "The number of words in the list is: %s." % (len(words),)
                        print "The shortest word in the list is: %s." % (sortedwords[0],)
                        print "The longest word in the list is: %s." % (sortedwords[-1],)


                        This produces:



                        The number of words in the list is: 10.
                        The shortest word in the list is: up.
                        The longest word in the list is: purple.






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 1 '14 at 1:44









                        John1024John1024

                        77k87197




                        77k87197

























                            2















                            Just use max and min functions having key as the length




                                y=
                            for names in range(4):
                            name=raw_input('Enter:')
                            y+=name,
                            s=max(y,key=len)
                            r=min(y,key=len)





                            share|improve this answer




























                              2















                              Just use max and min functions having key as the length




                                  y=
                              for names in range(4):
                              name=raw_input('Enter:')
                              y+=name,
                              s=max(y,key=len)
                              r=min(y,key=len)





                              share|improve this answer


























                                2












                                2








                                2








                                Just use max and min functions having key as the length




                                    y=
                                for names in range(4):
                                name=raw_input('Enter:')
                                y+=name,
                                s=max(y,key=len)
                                r=min(y,key=len)





                                share|improve this answer














                                Just use max and min functions having key as the length




                                    y=
                                for names in range(4):
                                name=raw_input('Enter:')
                                y+=name,
                                s=max(y,key=len)
                                r=min(y,key=len)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 5 '17 at 7:13









                                Ankit SharmaAnkit Sharma

                                394




                                394























                                    1














                                    You are so close - but I think the problem is incallFunctions(). You are mapping the functions in func_list2 to every string in the words array, not applying the function to the array as a whole. It was a good idea to use map, which is a powerful function, but you don't need to use it here. Here is code that I tested with a simple online interpreter. Try it. Good luck with whatever you are learning/ the project you are making!



                                    def bigWords(list=, *args):
                                    largestWord=""
                                    for word in list:
                                    if len(largestWord)<len(word):
                                    largestWord= word
                                    print "The longest word(s) in the list is %s." % largestWord
                                    return largestWord

                                    def smallWords(list=, *args):
                                    smallestWord = bigWords(list)
                                    for word in list:
                                    if len(smallestWord)> len(word):
                                    smallestWord = word
                                    print "The shortest word(s) in the list is: %s." % (smallestWord)


                                    def callFunctions():
                                    ###Words###
                                    words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                    wordLength=len(words)
                                    print "The amount of words is %d" % wordLength
                                    func_list2 = [bigWords, smallWords]
                                    for f in func_list2:
                                    f(words)

                                    callFunctions()





                                    share|improve this answer




























                                      1














                                      You are so close - but I think the problem is incallFunctions(). You are mapping the functions in func_list2 to every string in the words array, not applying the function to the array as a whole. It was a good idea to use map, which is a powerful function, but you don't need to use it here. Here is code that I tested with a simple online interpreter. Try it. Good luck with whatever you are learning/ the project you are making!



                                      def bigWords(list=, *args):
                                      largestWord=""
                                      for word in list:
                                      if len(largestWord)<len(word):
                                      largestWord= word
                                      print "The longest word(s) in the list is %s." % largestWord
                                      return largestWord

                                      def smallWords(list=, *args):
                                      smallestWord = bigWords(list)
                                      for word in list:
                                      if len(smallestWord)> len(word):
                                      smallestWord = word
                                      print "The shortest word(s) in the list is: %s." % (smallestWord)


                                      def callFunctions():
                                      ###Words###
                                      words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                      wordLength=len(words)
                                      print "The amount of words is %d" % wordLength
                                      func_list2 = [bigWords, smallWords]
                                      for f in func_list2:
                                      f(words)

                                      callFunctions()





                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        You are so close - but I think the problem is incallFunctions(). You are mapping the functions in func_list2 to every string in the words array, not applying the function to the array as a whole. It was a good idea to use map, which is a powerful function, but you don't need to use it here. Here is code that I tested with a simple online interpreter. Try it. Good luck with whatever you are learning/ the project you are making!



                                        def bigWords(list=, *args):
                                        largestWord=""
                                        for word in list:
                                        if len(largestWord)<len(word):
                                        largestWord= word
                                        print "The longest word(s) in the list is %s." % largestWord
                                        return largestWord

                                        def smallWords(list=, *args):
                                        smallestWord = bigWords(list)
                                        for word in list:
                                        if len(smallestWord)> len(word):
                                        smallestWord = word
                                        print "The shortest word(s) in the list is: %s." % (smallestWord)


                                        def callFunctions():
                                        ###Words###
                                        words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                        wordLength=len(words)
                                        print "The amount of words is %d" % wordLength
                                        func_list2 = [bigWords, smallWords]
                                        for f in func_list2:
                                        f(words)

                                        callFunctions()





                                        share|improve this answer













                                        You are so close - but I think the problem is incallFunctions(). You are mapping the functions in func_list2 to every string in the words array, not applying the function to the array as a whole. It was a good idea to use map, which is a powerful function, but you don't need to use it here. Here is code that I tested with a simple online interpreter. Try it. Good luck with whatever you are learning/ the project you are making!



                                        def bigWords(list=, *args):
                                        largestWord=""
                                        for word in list:
                                        if len(largestWord)<len(word):
                                        largestWord= word
                                        print "The longest word(s) in the list is %s." % largestWord
                                        return largestWord

                                        def smallWords(list=, *args):
                                        smallestWord = bigWords(list)
                                        for word in list:
                                        if len(smallestWord)> len(word):
                                        smallestWord = word
                                        print "The shortest word(s) in the list is: %s." % (smallestWord)


                                        def callFunctions():
                                        ###Words###
                                        words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                        wordLength=len(words)
                                        print "The amount of words is %d" % wordLength
                                        func_list2 = [bigWords, smallWords]
                                        for f in func_list2:
                                        f(words)

                                        callFunctions()






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Oct 1 '14 at 2:00









                                        theideasmiththeideasmith

                                        1,0902718




                                        1,0902718























                                            0














                                            First, you have an error in the code of function bigWords. You should compare with the length and not the word as follows



                                            def bigWords(list=, *args):
                                            largestWord=""
                                            largestLen=0
                                            for word in list:
                                            if largestLen<len(word):
                                            largestLen=len(word)
                                            largestWord=word
                                            print "The longest word(s) in the list is %s." % largestWord


                                            Second, to use the two functions, you need to call them for the list of words and not for each word:



                                            def callFunctions():
                                            ###Words###
                                            words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                            wordLength=lenList(words)
                                            print "The amount of words is %d" % wordLength
                                            func_list2 = [bigWords, smallWords]
                                            for f in func_list2:
                                            f(words)

                                            callFunctions()





                                            share|improve this answer
























                                            • How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                              – Shayd3
                                              Oct 1 '14 at 1:56











                                            • @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                              – wasserfeder
                                              Oct 1 '14 at 2:06
















                                            0














                                            First, you have an error in the code of function bigWords. You should compare with the length and not the word as follows



                                            def bigWords(list=, *args):
                                            largestWord=""
                                            largestLen=0
                                            for word in list:
                                            if largestLen<len(word):
                                            largestLen=len(word)
                                            largestWord=word
                                            print "The longest word(s) in the list is %s." % largestWord


                                            Second, to use the two functions, you need to call them for the list of words and not for each word:



                                            def callFunctions():
                                            ###Words###
                                            words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                            wordLength=lenList(words)
                                            print "The amount of words is %d" % wordLength
                                            func_list2 = [bigWords, smallWords]
                                            for f in func_list2:
                                            f(words)

                                            callFunctions()





                                            share|improve this answer
























                                            • How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                              – Shayd3
                                              Oct 1 '14 at 1:56











                                            • @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                              – wasserfeder
                                              Oct 1 '14 at 2:06














                                            0












                                            0








                                            0







                                            First, you have an error in the code of function bigWords. You should compare with the length and not the word as follows



                                            def bigWords(list=, *args):
                                            largestWord=""
                                            largestLen=0
                                            for word in list:
                                            if largestLen<len(word):
                                            largestLen=len(word)
                                            largestWord=word
                                            print "The longest word(s) in the list is %s." % largestWord


                                            Second, to use the two functions, you need to call them for the list of words and not for each word:



                                            def callFunctions():
                                            ###Words###
                                            words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                            wordLength=lenList(words)
                                            print "The amount of words is %d" % wordLength
                                            func_list2 = [bigWords, smallWords]
                                            for f in func_list2:
                                            f(words)

                                            callFunctions()





                                            share|improve this answer













                                            First, you have an error in the code of function bigWords. You should compare with the length and not the word as follows



                                            def bigWords(list=, *args):
                                            largestWord=""
                                            largestLen=0
                                            for word in list:
                                            if largestLen<len(word):
                                            largestLen=len(word)
                                            largestWord=word
                                            print "The longest word(s) in the list is %s." % largestWord


                                            Second, to use the two functions, you need to call them for the list of words and not for each word:



                                            def callFunctions():
                                            ###Words###
                                            words=["alpha","omega","up","down","over","under","purple","red","blue","green"]

                                            wordLength=lenList(words)
                                            print "The amount of words is %d" % wordLength
                                            func_list2 = [bigWords, smallWords]
                                            for f in func_list2:
                                            f(words)

                                            callFunctions()






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Oct 1 '14 at 1:43









                                            wasserfederwasserfeder

                                            37118




                                            37118













                                            • How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                              – Shayd3
                                              Oct 1 '14 at 1:56











                                            • @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                              – wasserfeder
                                              Oct 1 '14 at 2:06



















                                            • How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                              – Shayd3
                                              Oct 1 '14 at 1:56











                                            • @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                              – wasserfeder
                                              Oct 1 '14 at 2:06

















                                            How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                            – Shayd3
                                            Oct 1 '14 at 1:56





                                            How would I go about finding the smallest word? My output keeps giving me "e" as the smallest word.

                                            – Shayd3
                                            Oct 1 '14 at 1:56













                                            @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                            – wasserfeder
                                            Oct 1 '14 at 2:06





                                            @Shayd3 you also have a bug in that function. Replace the first line in the if block with smallestLen=len(word).

                                            – wasserfeder
                                            Oct 1 '14 at 2:06











                                            0














                                            Try this simple solution:



                                            def find_longest_and_shortest_word(list_of_words):
                                            longest_word = list_of_words[0]
                                            shortest_word = list_of_words[0]
                                            for word in list_of_words:
                                            if len(longest_word) < len(word):
                                            longest_word = word
                                            if len(shortest_word) > len(word):
                                            shortest_word = word
                                            print(f'The longest word is: {longest_word}')
                                            print(f'The shortest word is: {shortest_word}')
                                            return longest_word, shortest_word





                                            share|improve this answer




























                                              0














                                              Try this simple solution:



                                              def find_longest_and_shortest_word(list_of_words):
                                              longest_word = list_of_words[0]
                                              shortest_word = list_of_words[0]
                                              for word in list_of_words:
                                              if len(longest_word) < len(word):
                                              longest_word = word
                                              if len(shortest_word) > len(word):
                                              shortest_word = word
                                              print(f'The longest word is: {longest_word}')
                                              print(f'The shortest word is: {shortest_word}')
                                              return longest_word, shortest_word





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Try this simple solution:



                                                def find_longest_and_shortest_word(list_of_words):
                                                longest_word = list_of_words[0]
                                                shortest_word = list_of_words[0]
                                                for word in list_of_words:
                                                if len(longest_word) < len(word):
                                                longest_word = word
                                                if len(shortest_word) > len(word):
                                                shortest_word = word
                                                print(f'The longest word is: {longest_word}')
                                                print(f'The shortest word is: {shortest_word}')
                                                return longest_word, shortest_word





                                                share|improve this answer













                                                Try this simple solution:



                                                def find_longest_and_shortest_word(list_of_words):
                                                longest_word = list_of_words[0]
                                                shortest_word = list_of_words[0]
                                                for word in list_of_words:
                                                if len(longest_word) < len(word):
                                                longest_word = word
                                                if len(shortest_word) > len(word):
                                                shortest_word = word
                                                print(f'The longest word is: {longest_word}')
                                                print(f'The shortest word is: {shortest_word}')
                                                return longest_word, shortest_word






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jan 1 at 22:39









                                                2RMalinowski2RMalinowski

                                                11112




                                                11112























                                                    0














                                                    you can sort you list using sorted() function that allows you to sort the list by the length of the strings in it.for that you need to add key=len so the function will sort by length and not by alphabecit order. you also need to give to the function reverse=true
                                                    so it will be easier to access to the longest string (it will be in [0] at the list)



                                                    def longest(my_list):
                                                    my_list = sorted(my_list, key=len, reverse=True)
                                                    return my_list[0]


                                                    list1 = ['aaa', 'bbbb', 'cccccc', 'd']
                                                    print(longest(list1))






                                                    share|improve this answer




























                                                      0














                                                      you can sort you list using sorted() function that allows you to sort the list by the length of the strings in it.for that you need to add key=len so the function will sort by length and not by alphabecit order. you also need to give to the function reverse=true
                                                      so it will be easier to access to the longest string (it will be in [0] at the list)



                                                      def longest(my_list):
                                                      my_list = sorted(my_list, key=len, reverse=True)
                                                      return my_list[0]


                                                      list1 = ['aaa', 'bbbb', 'cccccc', 'd']
                                                      print(longest(list1))






                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        you can sort you list using sorted() function that allows you to sort the list by the length of the strings in it.for that you need to add key=len so the function will sort by length and not by alphabecit order. you also need to give to the function reverse=true
                                                        so it will be easier to access to the longest string (it will be in [0] at the list)



                                                        def longest(my_list):
                                                        my_list = sorted(my_list, key=len, reverse=True)
                                                        return my_list[0]


                                                        list1 = ['aaa', 'bbbb', 'cccccc', 'd']
                                                        print(longest(list1))






                                                        share|improve this answer













                                                        you can sort you list using sorted() function that allows you to sort the list by the length of the strings in it.for that you need to add key=len so the function will sort by length and not by alphabecit order. you also need to give to the function reverse=true
                                                        so it will be easier to access to the longest string (it will be in [0] at the list)



                                                        def longest(my_list):
                                                        my_list = sorted(my_list, key=len, reverse=True)
                                                        return my_list[0]


                                                        list1 = ['aaa', 'bbbb', 'cccccc', 'd']
                                                        print(longest(list1))







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Feb 28 at 17:16









                                                        טל חודורקובסקיטל חודורקובסקי

                                                        11




                                                        11






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26132770%2fpython-finding-longest-shortest-words-in-a-list-and-calling-them-in-a-function%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                                                            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                                                            A Topological Invariant for $pi_3(U(n))$