Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function called starts_with(prefix, wordlist) that takes as inputs a string prefix and a list of strings wordlist, and that uses a list comprehension

Write a function called starts_with(prefix, wordlist) that takes as inputs a string prefix and a list of strings wordlist, and that uses a list comprehension to return a list consisting of all words from wordlist that begin with prefix. For example:

>>> starts_with('fun', ['functions', 'are', 'really', 'fun!']) result: ['functions', 'fun!'] >>> starts_with('on', ['only', 'functions', 'on', 'the', 'brain']) result: ['only', 'on'] # note that 'functions' is *not* included >>> names = ['Alex', 'Adlai', 'Alison', 'Amalia', 'Anbita'] >>> starts_with('A', names) result: ['Alex', 'Adlai', 'Alison', 'Amalia', 'Anbita'] >>> starts_with('Al', names) result: ['Alex', 'Alison'] >>> starts_with('D', names) result: [] 

Hints:

  • Your list comprehension will need an if clause.

  • Make sure that you only include words that start with the specified prefix. For instance, in the second example above, the string 'functions' is not included in the return value, because the string 'on' is in the middle of 'functions', rather than at the start. As a result, you wont be able to use the in operator to test for the presence of the prefix in a word.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Does talking equal effective communication?

Answered: 1 week ago

Question

Q.No.1 Explain Large scale map ? Q.No.2 Explain small scale map ?

Answered: 1 week ago