FOR PYTHON: Write a function n_letter () that takes a list lst of strings and a
ID: 640907 • Letter: F
Question
FOR PYTHON: Write a function n_letter() that takes a list lst of strings and a non-negative (>= 0) integer n as a parameter and returns a new list containing all of the strings in lst that have length n. If n is negative or lst is empty the function should return an empty list. The original list should not be modified. The following shows several sample runs of the function:
Le Python 3.4.1 Shell File Edit Shell Debug OtiosWindows Help >>> nletter (I' dog, 'letter', 'stop, 'door, 'cat, 'bus,'floor'1,3) ' dogcat', 'bus' >>> nletter (l' dog, letterstopdoorcat', 'bus',floor'],4) letter, stop', 'door, eat','bus floor'. 4, cat', 'bus', floor'],5) 2) . >>> nl etter (' dog,'letter stop'door terit . letter','stop ,'door, eat'. 'bus, .tloor . >>> nletter ([' dog, letterstop'door cat', 'bus', floor'],2) C1 >>n letter5) C] >>> n_letter('oneto,thr, 3) C1 log . Ln: 63 Col: 4Explanation / Answer
def n_letter(lst, n):
tList = []
if len(lst) == 0 or n < 1:
return tList
else:
for i in lst:
if len(i) == n:
tList.append(i)
return tList
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.