I have a dictionary like the following in python 3:
ss = {'a':'2', 'b','3'}
I want to convert all he values to int using map
function, and I wrote something like this:
list(map(lambda key,val: int(val), ss.items())))
but the python complains:
TypeError: () missing 1 required positional argument: 'val'
My question is how can I write a lambda
function with two inputs (E.g. key and val)
ss.items()
will give an iterable, which gives tuples on every iteration. In yourlambda
function, you have defined it to accept two parameters, but the tuple will be treated as a single argument. So there is no value to be passed to the second parameter.You can fix it like this
If you are ignoring the keys anyway, simply use
ss.values()
like thisOtherwise, as suggested by Ashwini Chaudhary, using
itertools.starmap
,I would prefer the List comprehension way
In Python 2.x, you could have done that like this
This feature is called Tuple parameter unpacking. But this is removed in Python 3.x. Read more about it in PEP-3113