python - How to write a Lambda with a for loop in it -
i trying make empty list same length number of letters in string:
string = "string" list = [] #insert lambda here produces following: # ==> list = [none, none, none, none, none, none]
the lambda should equivalent of code:
for in range(len(string)): list.append(none)
i have tried following lambda:
lambda x: in range(len(string)): list.append(none)
however keeps replying syntax error
, highlights word for
.
what wrong lambda?
why not multiplying?
>>> lst = [none]*5 >>> lst [none, none, none, none, none] >>> lst[1] = 4 >>> lst [none, 4, none, none, none]
why not list comprehension?
>>> lst = [none x in range(5)] >>> lst [none, none, none, none, none] >>> lst[3] = 9 >>> lst [none, none, none, 9, none]
but… lambda:
>>> k=lambda x: [none]*x >>> lst = k(5) >>> lst [none, none, none, none, none] >>> lst[4]=8 >>> lst [none, none, none, none, 8]
Comments
Post a Comment