python - return a new list from a list stopped at certain location -


i can't code make work life of me.

in exercise function receive 1 parameter, list of strings. function make , return new list includes copies of strings parameter to, not including string kahn. kahn in list.

expected result:

words_up_to_kahn(["kirk", "spock", "luke", "charlie", "kahn", "bob"]) -> ["kirk", "spock", "luke", "charlie"] words_up_to_kahn(["ernie", "kahn", "bert", "teal'c"]) -> ["ernie"] 

examples: this?

return words[0:kahn] 

or this:

def words_up_to_kahn(words):     new = ""     c = 0     while words[c] != "kahn":         new = new + words[c]         c = c + 1     return new 

or else?

this simplest solution believe.

conditional statements , .index("kahn") friends:

def up_to_kahn(lst):     return lst if ("kahn" not in lst) else lst[:lst.index("kahn")]  assert up_to_kahn(["kirk", "spock", "luke", "charlie", "kahn", "bob"]) == \     ["kirk", "spock", "luke", "charlie"] assert up_to_kahn(["ernie", "kahn", "bert", "teal'c"]) == ["ernie"] 

also copy-pasted textbook. please don't use stack overflow homework-answers site. said, here's answer.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -