129

How do I apply a function to the list of variable inputs? For e.g. the filter function returns true values but not the actual output of the function.

from string import upper
mylis=['this is test', 'another test']

filter(upper, mylis)
['this is test', 'another test']

The expected output is :

['THIS IS TEST', 'ANOTHER TEST']

I know upper is built-in. This is just an example.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
shantanuo
  • 30,102
  • 75
  • 225
  • 364

3 Answers3

146

I think you mean to use map instead of filter:

>>> from string import upper
>>> mylis=['this is test', 'another test']
>>> map(upper, mylis)
['THIS IS TEST', 'ANOTHER TEST']

Even simpler, you could use str.upper instead of importing from string (thanks to @alecxe):

>>> map(str.upper, mylis)
['THIS IS TEST', 'ANOTHER TEST']

In Python 2.x, map constructs a new list by applying a given function to every element in a list. filter constructs a new list by restricting to elements that evaluate to True with a given function.

In Python 3.x, map and filter construct iterators instead of lists, so if you are using Python 3.x and require a list the list comprehension approach would be better suited.

mgilson
  • 283,004
  • 58
  • 591
  • 667
mdml
  • 21,241
  • 8
  • 53
  • 64
  • 5
    `map(str.upper, mylis)` would work too, would help to avoid a `string` import. – alecxe Aug 01 '14 at 14:29
  • 5
    Note that `map` only constructs a `list` on python2.x -- On python3.x it returns an iterator. Frequently that doesn't matter, but _if_ you _need_ a list as output, then it's probably better to use a list comprehension as in the other answer. – mgilson Aug 01 '14 at 14:37
  • 1
    Is approach the same for side effecting functions? – Ivan Balashov Sep 25 '19 at 06:20
  • 13
    In Python 3.x, you can do `list(map(str.upper, mylis))`, if you [want list](https://stackoverflow.com/a/1303354/7211502). – BlueManCZ Feb 23 '20 at 23:00
145

Or, alternatively, you can take a list comprehension approach:

>>> mylis = ['this is test', 'another test']
>>> [item.upper() for item in mylis]
['THIS IS TEST', 'ANOTHER TEST']
wjandrea
  • 23,210
  • 7
  • 49
  • 68
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
6

Sometimes you need to apply a function to the members of a list in place. The following code worked for me:

>>> def func(a, i):
...     a[i] = a[i].lower()
>>> a = ['TEST', 'TEXT']
>>> list(map(lambda i:func(a, i), range(0, len(a))))
[None, None]
>>> print(a)
['test', 'text']

Please note, the output of map() is passed to the list constructor to ensure the list is converted in Python 3. The returned list filled with None values should be ignored, since our purpose was to convert list a in place

efimp
  • 141
  • 2
  • 1
  • 1
    A suggestion: don't create a list filled with `None`s only to throw it away immediately, use `any` instead of `list` around `map`. Also, use `range` with only one argument, the range will start from 0, same as list indices. Final result: `any(map(lambda i:func(a, i), range(len(a))))`. This expression will return `False` , but that can be ignored. – ack Mar 19 '21 at 18:49
  • @ack Your suggestion is good where you care about the status of each element (e.g. you really do want to check if any of the results are not `None`) but not sure why else you would want to go to the trouble of looping through the `None`s to check them, only to throw away a different result? This is only an consideration when using the interpreter. – scign Jan 24 '22 at 20:32