Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list func in lambda
#1
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] >>>
Reply
#2
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,323 Nov-11-2022, 10:45 PM
Last Post: snippsat
  List comprehension and Lambda cametan 2 2,235 Jun-08-2021, 08:29 AM
Last Post: cametan
  Func Animation not displaying my live graph jotalo 0 1,552 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,091 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,725 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Multiple lambda functions in zipped list not executing psolar 0 1,596 Feb-13-2020, 12:53 PM
Last Post: psolar
  call func from dict mcmxl22 3 2,855 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 3,589 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,340 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 6,006 Jan-11-2019, 04:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020