Hi Viet, map applies the function to each of the elements of the list you provide.
It would be roughly equivalent to: [add_all_elements(x) for x in alist] It may help you to consider the term and function "map" from the view of linear algebra. Apparently it's a common term: https://en.wikipedia.org/wiki/Map_%28higher-order_function%29?wprov=sfla1 HTH On September 21, 2018 11:29:41 PM GMT+02:00, Viet Nguyen via Python-list <python-list@python.org> wrote: >Hi, > >I want to add up all of the list elements. But when I use the "map" >function, it didn't seem to work as I expect. Could someone point out >how "map" can be applied here then? > >def add_all_elements (*args): > total = 0 > for i in args: > print(type(i)) > print("i = %s" % i) > print("BEFORE total = %s" % total) > total += int(i) > print("AFTER total = %s\n" % total) > print("FINAL total = %s\n" % total) > return total > > >alist = ['2', '09', '49'] > > >## this one works Okay > >add_all_elements(*alist) ><class 'str'> >i = 2 >BEFORE total = 0 >AFTER total = 2 > ><class 'str'> >i = 09 >BEFORE total = 2 >AFTER total = 11 > ><class 'str'> >i = 49 >BEFORE total = 11 >AFTER total = 60 > >FINAL total = 60 > >======== >## Why is this NOT Okay when I use map ?? What must I change ? > >>>> list(map(add_all_elements,alist)) ><class 'str'> >i = 2 >BEFORE total = 0 >AFTER total = 2 > >FINAL total = 2 > ><class 'str'> >i = 09 >BEFORE total = 0 >AFTER total = 9 > >FINAL total = 9 > ><class 'str'> >i = 49 >BEFORE total = 0 >AFTER total = 49 > >FINAL total = 49 > >[2, 9, 49] > > >Thanks, >Viet >-- >https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list