Python: Finding Longest/Shortest Words In a List and Calling Them in a Function
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
add a comment |
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
add a comment |
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
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
python function loops for-loop maps
asked Oct 1 '14 at 1:30
Shayd3Shayd3
611110
611110
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
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.
add a comment |
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)
add a comment |
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()
add a comment |
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()
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
add a comment |
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
add a comment |
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))
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Oct 1 '14 at 1:44
John1024John1024
77k87197
77k87197
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Jul 5 '17 at 7:13
Ankit SharmaAnkit Sharma
394
394
add a comment |
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
answered Oct 1 '14 at 2:00
theideasmiththeideasmith
1,0902718
1,0902718
add a comment |
add a comment |
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()
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
add a comment |
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()
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
add a comment |
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()
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()
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 1 at 22:39
2RMalinowski2RMalinowski
11112
11112
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Feb 28 at 17:16
טל חודורקובסקיטל חודורקובסקי
11
11
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%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
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