filter vs map vs reduce functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python24
    New Member
    • Nov 2007
    • 13

    filter vs map vs reduce functions

    I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??

    Thanks in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by python24
    I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??

    Thanks in advance
    Here are examples of each:[code=Python]>>> def imp_to_mm(inche s):
    ... return inches * 25.4
    ...
    >>> inchesList = [1,2,12,24,36]
    >>> map(imp_to_mm, inchesList)
    [25.399999999999 999, 50.799999999999 997, 304.79999999999 995, 609.59999999999 991, 914.39999999999 998]
    >>> def is_str(item):
    ... if isinstance(item , str):
    ... return True
    ... return False
    ...
    >>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
    ['a', 'BBB', 'A string']
    >>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8 ,9,10])
    55
    >>> [/code]map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension.[code=Python]>>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)][/code]reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.

    Comment

    • python24
      New Member
      • Nov 2007
      • 13

      #3
      Originally posted by bvdet
      Here are examples of each:[code=Python]>>> def imp_to_mm(inche s):
      ... return inches * 25.4
      ...
      >>> inchesList = [1,2,12,24,36]
      >>> map(imp_to_mm, inchesList)
      [25.399999999999 999, 50.799999999999 997, 304.79999999999 995, 609.59999999999 991, 914.39999999999 998]
      >>> def is_str(item):
      ... if isinstance(item , str):
      ... return True
      ... return False
      ...
      >>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
      ['a', 'BBB', 'A string']
      >>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8 ,9,10])
      55
      >>> [/code]map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension.[code=Python]>>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)][/code]reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.

      I know this forum will never disappoint and will clarify every minute doubts without criticizing.
      Thank u so much

      Comment

      Working...