Python Forum

Full Version: list func in lambda
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
is list() new in python3? I ask because my lambda functions will provide my lambda object name without list() only. But won't print without list(). Many lambda examples on the net work without list() [http://www.secnetix.de/olli/Python/lambd...tions.hawk]

Output:
>>> sentence ='it is raining' >>> words = sentence.split() >>> print(words) ['it', 'is', 'raining'] >>> lengths = map(lambda word: len(word),words) >>> print(lengths) <map object at 0x7f82ad3a0b70> >>> print(list(lengths)) [2, 2, 7] >>>
in python 3 map would produce/return iterator. that's why you need to use list to convert to list object.
without it you still can iterate over elements e.g.
for word_length in lengths:
    print(word_length)

Also, note that you don't need lambda in this case
>>> words = ['it', 'is', 'raining']
>>> list(map(len, words))
[2, 2, 7]