Question

python code that Returns a LIST that contains ALL WORDS in word_list that have the specified...

python code that Returns a LIST that contains ALL WORDS in word_list that have the specified length and Returns an empty list if there are no such words

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Go through the screenshots for proper indentation.

-------------------------------------Code----------------------------------

#Defining a function which takes a list and specific length as arguments.

def lenList(word_list, length):

if len(word_list) == 0: #Checking if the list is empty or not
return None
else:
# Using list comprehension and storing words which length is equal
# to the specified lenght
newList = [word for word in word_list if len(word) == length ]
print(newList) #Printing the list
return newList #returning the list.

#If no words have the specified length, it will return an empty list.

wordList = ['Apple', 'Sit', 'Tony', 'Manny', 'Captain', 'Boy']#Defining a list

lenList(wordList, 10) #Calling the function and passing a specific length

X scs.py - C:\Users\Manish AppData\Local\Programs\Python Python38\scs.py (3.8.2) File Edit Format Run Options Window Help #DeOutput:

Х Python 3.8.2 Shell - File Edit Shell Debug Options Window Help Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [M

-----------------------------------------------------------

*scs.py - C:\Users\Manish\AppData\Local\Programs\Python Python38\scs.py (3.8.2)* File Edit Format Run Options Window Help #DeOutput:

Python 3.8.2 Shell File Edit Shell Debug Options Window Help Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v----------------------------------------------------------------

scs.py - C:\Users\Manish AppData\Local\Programs\Python Python38\scs.py (3.8.2) File Edit Format Run Options Window Help #DefiOutput:

Returned an empty list if no word length matched the specified length.

Python 3.8.2 Shell File Edit Shell Debug Options Window Help Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v

Add a comment
Know the answer?
Add Answer to:
python code that Returns a LIST that contains ALL WORDS in word_list that have the specified...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT