find first string starting with vowel in LIST Python
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
add a comment |
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
add a comment |
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
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
python python-3.x
edited Jan 2 at 5:49
Barbaros Özhan
14.2k71634
14.2k71634
asked Jan 2 at 5:05
F HamadF Hamad
132
132
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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)
1
What ifiis 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
add a comment |
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'
this is working. thank you. What does s[0] mean with "and" in the syntax?
– F Hamad
Jan 2 at 6:07
@FHamadif smeans if the string is not empty;andsimply 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
|
show 2 more comments
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))
add a comment |
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'
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
1
What ifiis 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
add a comment |
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)
1
What ifiis 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
add a comment |
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)
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)
edited Jan 2 at 5:27
answered Jan 2 at 5:13
AkshayNevrekarAkshayNevrekar
5,28291940
5,28291940
1
What ifiis 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
add a comment |
1
What ifiis 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
add a comment |
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'
this is working. thank you. What does s[0] mean with "and" in the syntax?
– F Hamad
Jan 2 at 6:07
@FHamadif smeans if the string is not empty;andsimply 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
|
show 2 more comments
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'
this is working. thank you. What does s[0] mean with "and" in the syntax?
– F Hamad
Jan 2 at 6:07
@FHamadif smeans if the string is not empty;andsimply 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
|
show 2 more comments
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'
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'
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
@FHamadif smeans if the string is not empty;andsimply 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
|
show 2 more comments
this is working. thank you. What does s[0] mean with "and" in the syntax?
– F Hamad
Jan 2 at 6:07
@FHamadif smeans if the string is not empty;andsimply 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
|
show 2 more comments
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))
add a comment |
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))
add a comment |
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))
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))
answered Jan 2 at 5:25
GeekSambhuGeekSambhu
699819
699819
add a comment |
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
answered Jan 2 at 5:28
VicrobotVicrobot
793518
793518
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
