find first string starting with vowel in LIST Python












2















I am brand new to python -



I have to build a function called 'first__vowel'. ACCEPT a list of strings as input
and RETURN the first string that starts with a lowercase vowel ("a","e","i","o", or "u"). if no string starts with vowel, RETURN the empty string ("").



Can you help build this function.



Thanks










share|improve this question





























    2















    I am brand new to python -



    I have to build a function called 'first__vowel'. ACCEPT a list of strings as input
    and RETURN the first string that starts with a lowercase vowel ("a","e","i","o", or "u"). if no string starts with vowel, RETURN the empty string ("").



    Can you help build this function.



    Thanks










    share|improve this question



























      2












      2








      2








      I am brand new to python -



      I have to build a function called 'first__vowel'. ACCEPT a list of strings as input
      and RETURN the first string that starts with a lowercase vowel ("a","e","i","o", or "u"). if no string starts with vowel, RETURN the empty string ("").



      Can you help build this function.



      Thanks










      share|improve this question
















      I am brand new to python -



      I have to build a function called 'first__vowel'. ACCEPT a list of strings as input
      and RETURN the first string that starts with a lowercase vowel ("a","e","i","o", or "u"). if no string starts with vowel, RETURN the empty string ("").



      Can you help build this function.



      Thanks







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 5:49









      Barbaros Özhan

      14.2k71634




      14.2k71634










      asked Jan 2 at 5:05









      F HamadF Hamad

      132




      132
























          4 Answers
          4






          active

          oldest

          votes


















          0














          You need:



          def first_vowel(lst):
          # iterate over list using for loop
          for i in lst:
          # check if first letter is vowel
          if i and i[0] in ['a','e','i','o','u']:
          return i
          return ""

          k = ['sad','dad','mad','asd','eas']
          print(first_vowel(k))


          Or You can also use regex



          import re

          def first_vow(lst):
          pat = re.compile(r'^[aeiou][a-zA-Z]*')
          for i in lst:
          match = re.match(pat, lst)
          if match:
          return i
          return ""

          k = ['sad','Aad','mad','','asd','eas']
          first_vow(k)





          share|improve this answer





















          • 1





            What if i is empty? i[0] would make things crashed...

            – duong_dajgja
            Jan 2 at 5:17











          • @duong_dajgja you are right!! Edited my answer :-)

            – AkshayNevrekar
            Jan 2 at 5:19





















          2














          Check this:



          vowels = ["a", "e", "i", "o", "u"]

          def first_vowel(ss):
          for s in ss:
          if s and s[0] in vowels:
          return s
          return ""


          Test:



          first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
          'aBD'





          share|improve this answer
























          • this is working. thank you. What does s[0] mean with "and" in the syntax?

            – F Hamad
            Jan 2 at 6:07











          • @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

            – duong_dajgja
            Jan 2 at 6:11













          • @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

            – duong_dajgja
            Jan 2 at 7:19











          • i used this answer. is there a way to mark accepted? please let me know how, new to this site.

            – F Hamad
            Jan 2 at 14:58











          • @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

            – duong_dajgja
            Jan 2 at 15:04





















          0














          You can always use str.startswith() function too:



          def first_vowel(lst):
          for elem in lst:
          if elem.startswith(('a','e','i','o','u')):
          return elem
          return ''
          k = ['', 'b','sad','dad','mad','asd','eas']
          print(first_vowel(k))





          share|improve this answer































            0














            l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

            def first_vowel(var):
            for i in var:
            if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


            first_vowel(l1) # output-> 'its'





            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%2f54001454%2ffind-first-string-starting-with-vowel-in-list-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You need:



              def first_vowel(lst):
              # iterate over list using for loop
              for i in lst:
              # check if first letter is vowel
              if i and i[0] in ['a','e','i','o','u']:
              return i
              return ""

              k = ['sad','dad','mad','asd','eas']
              print(first_vowel(k))


              Or You can also use regex



              import re

              def first_vow(lst):
              pat = re.compile(r'^[aeiou][a-zA-Z]*')
              for i in lst:
              match = re.match(pat, lst)
              if match:
              return i
              return ""

              k = ['sad','Aad','mad','','asd','eas']
              first_vow(k)





              share|improve this answer





















              • 1





                What if i is empty? i[0] would make things crashed...

                – duong_dajgja
                Jan 2 at 5:17











              • @duong_dajgja you are right!! Edited my answer :-)

                – AkshayNevrekar
                Jan 2 at 5:19


















              0














              You need:



              def first_vowel(lst):
              # iterate over list using for loop
              for i in lst:
              # check if first letter is vowel
              if i and i[0] in ['a','e','i','o','u']:
              return i
              return ""

              k = ['sad','dad','mad','asd','eas']
              print(first_vowel(k))


              Or You can also use regex



              import re

              def first_vow(lst):
              pat = re.compile(r'^[aeiou][a-zA-Z]*')
              for i in lst:
              match = re.match(pat, lst)
              if match:
              return i
              return ""

              k = ['sad','Aad','mad','','asd','eas']
              first_vow(k)





              share|improve this answer





















              • 1





                What if i is empty? i[0] would make things crashed...

                – duong_dajgja
                Jan 2 at 5:17











              • @duong_dajgja you are right!! Edited my answer :-)

                – AkshayNevrekar
                Jan 2 at 5:19
















              0












              0








              0







              You need:



              def first_vowel(lst):
              # iterate over list using for loop
              for i in lst:
              # check if first letter is vowel
              if i and i[0] in ['a','e','i','o','u']:
              return i
              return ""

              k = ['sad','dad','mad','asd','eas']
              print(first_vowel(k))


              Or You can also use regex



              import re

              def first_vow(lst):
              pat = re.compile(r'^[aeiou][a-zA-Z]*')
              for i in lst:
              match = re.match(pat, lst)
              if match:
              return i
              return ""

              k = ['sad','Aad','mad','','asd','eas']
              first_vow(k)





              share|improve this answer















              You need:



              def first_vowel(lst):
              # iterate over list using for loop
              for i in lst:
              # check if first letter is vowel
              if i and i[0] in ['a','e','i','o','u']:
              return i
              return ""

              k = ['sad','dad','mad','asd','eas']
              print(first_vowel(k))


              Or You can also use regex



              import re

              def first_vow(lst):
              pat = re.compile(r'^[aeiou][a-zA-Z]*')
              for i in lst:
              match = re.match(pat, lst)
              if match:
              return i
              return ""

              k = ['sad','Aad','mad','','asd','eas']
              first_vow(k)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 2 at 5:27

























              answered Jan 2 at 5:13









              AkshayNevrekarAkshayNevrekar

              5,28291940




              5,28291940








              • 1





                What if i is empty? i[0] would make things crashed...

                – duong_dajgja
                Jan 2 at 5:17











              • @duong_dajgja you are right!! Edited my answer :-)

                – AkshayNevrekar
                Jan 2 at 5:19
















              • 1





                What if i is empty? i[0] would make things crashed...

                – duong_dajgja
                Jan 2 at 5:17











              • @duong_dajgja you are right!! Edited my answer :-)

                – AkshayNevrekar
                Jan 2 at 5:19










              1




              1





              What if i is empty? i[0] would make things crashed...

              – duong_dajgja
              Jan 2 at 5:17





              What if i is empty? i[0] would make things crashed...

              – duong_dajgja
              Jan 2 at 5:17













              @duong_dajgja you are right!! Edited my answer :-)

              – AkshayNevrekar
              Jan 2 at 5:19







              @duong_dajgja you are right!! Edited my answer :-)

              – AkshayNevrekar
              Jan 2 at 5:19















              2














              Check this:



              vowels = ["a", "e", "i", "o", "u"]

              def first_vowel(ss):
              for s in ss:
              if s and s[0] in vowels:
              return s
              return ""


              Test:



              first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
              'aBD'





              share|improve this answer
























              • this is working. thank you. What does s[0] mean with "and" in the syntax?

                – F Hamad
                Jan 2 at 6:07











              • @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

                – duong_dajgja
                Jan 2 at 6:11













              • @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

                – duong_dajgja
                Jan 2 at 7:19











              • i used this answer. is there a way to mark accepted? please let me know how, new to this site.

                – F Hamad
                Jan 2 at 14:58











              • @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

                – duong_dajgja
                Jan 2 at 15:04


















              2














              Check this:



              vowels = ["a", "e", "i", "o", "u"]

              def first_vowel(ss):
              for s in ss:
              if s and s[0] in vowels:
              return s
              return ""


              Test:



              first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
              'aBD'





              share|improve this answer
























              • this is working. thank you. What does s[0] mean with "and" in the syntax?

                – F Hamad
                Jan 2 at 6:07











              • @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

                – duong_dajgja
                Jan 2 at 6:11













              • @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

                – duong_dajgja
                Jan 2 at 7:19











              • i used this answer. is there a way to mark accepted? please let me know how, new to this site.

                – F Hamad
                Jan 2 at 14:58











              • @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

                – duong_dajgja
                Jan 2 at 15:04
















              2












              2








              2







              Check this:



              vowels = ["a", "e", "i", "o", "u"]

              def first_vowel(ss):
              for s in ss:
              if s and s[0] in vowels:
              return s
              return ""


              Test:



              first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
              'aBD'





              share|improve this answer













              Check this:



              vowels = ["a", "e", "i", "o", "u"]

              def first_vowel(ss):
              for s in ss:
              if s and s[0] in vowels:
              return s
              return ""


              Test:



              first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
              'aBD'






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 2 at 5:15









              duong_dajgjaduong_dajgja

              1,72611636




              1,72611636













              • this is working. thank you. What does s[0] mean with "and" in the syntax?

                – F Hamad
                Jan 2 at 6:07











              • @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

                – duong_dajgja
                Jan 2 at 6:11













              • @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

                – duong_dajgja
                Jan 2 at 7:19











              • i used this answer. is there a way to mark accepted? please let me know how, new to this site.

                – F Hamad
                Jan 2 at 14:58











              • @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

                – duong_dajgja
                Jan 2 at 15:04





















              • this is working. thank you. What does s[0] mean with "and" in the syntax?

                – F Hamad
                Jan 2 at 6:07











              • @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

                – duong_dajgja
                Jan 2 at 6:11













              • @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

                – duong_dajgja
                Jan 2 at 7:19











              • i used this answer. is there a way to mark accepted? please let me know how, new to this site.

                – F Hamad
                Jan 2 at 14:58











              • @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

                – duong_dajgja
                Jan 2 at 15:04



















              this is working. thank you. What does s[0] mean with "and" in the syntax?

              – F Hamad
              Jan 2 at 6:07





              this is working. thank you. What does s[0] mean with "and" in the syntax?

              – F Hamad
              Jan 2 at 6:07













              @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

              – duong_dajgja
              Jan 2 at 6:11







              @FHamad if s means if the string is not empty; and simply AND; s[0] is the first character in the string. I recommend you to take a simple python course, e.g., at tutorialspoint.com/python/python_strings.htm

              – duong_dajgja
              Jan 2 at 6:11















              @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

              – duong_dajgja
              Jan 2 at 7:19





              @FHamad If this helps please consider to mark this answer as accepted (if you don't mind). Thanks!

              – duong_dajgja
              Jan 2 at 7:19













              i used this answer. is there a way to mark accepted? please let me know how, new to this site.

              – F Hamad
              Jan 2 at 14:58





              i used this answer. is there a way to mark accepted? please let me know how, new to this site.

              – F Hamad
              Jan 2 at 14:58













              @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

              – duong_dajgja
              Jan 2 at 15:04







              @FHamad There is V mark right under <up> 3 <down> next to my answer. Please click on the V mark and it will turn green like this i.stack.imgur.com/LkiIZ.png

              – duong_dajgja
              Jan 2 at 15:04













              0














              You can always use str.startswith() function too:



              def first_vowel(lst):
              for elem in lst:
              if elem.startswith(('a','e','i','o','u')):
              return elem
              return ''
              k = ['', 'b','sad','dad','mad','asd','eas']
              print(first_vowel(k))





              share|improve this answer




























                0














                You can always use str.startswith() function too:



                def first_vowel(lst):
                for elem in lst:
                if elem.startswith(('a','e','i','o','u')):
                return elem
                return ''
                k = ['', 'b','sad','dad','mad','asd','eas']
                print(first_vowel(k))





                share|improve this answer


























                  0












                  0








                  0







                  You can always use str.startswith() function too:



                  def first_vowel(lst):
                  for elem in lst:
                  if elem.startswith(('a','e','i','o','u')):
                  return elem
                  return ''
                  k = ['', 'b','sad','dad','mad','asd','eas']
                  print(first_vowel(k))





                  share|improve this answer













                  You can always use str.startswith() function too:



                  def first_vowel(lst):
                  for elem in lst:
                  if elem.startswith(('a','e','i','o','u')):
                  return elem
                  return ''
                  k = ['', 'b','sad','dad','mad','asd','eas']
                  print(first_vowel(k))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 5:25









                  GeekSambhuGeekSambhu

                  699819




                  699819























                      0














                      l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

                      def first_vowel(var):
                      for i in var:
                      if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


                      first_vowel(l1) # output-> 'its'





                      share|improve this answer




























                        0














                        l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

                        def first_vowel(var):
                        for i in var:
                        if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


                        first_vowel(l1) # output-> 'its'





                        share|improve this answer


























                          0












                          0








                          0







                          l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

                          def first_vowel(var):
                          for i in var:
                          if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


                          first_vowel(l1) # output-> 'its'





                          share|improve this answer













                          l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

                          def first_vowel(var):
                          for i in var:
                          if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


                          first_vowel(l1) # output-> 'its'






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 2 at 5:28









                          VicrobotVicrobot

                          793518




                          793518






























                              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%2f54001454%2ffind-first-string-starting-with-vowel-in-list-python%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

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith