Main Content

Pass Python Function to Python map Function

This example shows how to display the length of each word in a list.

Create a list of days of the work week.

days = py.list({'Monday','Tuesday','Wednesday','Thursday','Friday'});

Display the length of each word by applying the Python® len function to the py.map function. To indicate that py.len is a function, use the MATLAB® function handle notation @.

py.map(@py.len,days)
ans = 

  Python list with no properties.

    [6, 7, 9, 8, 6]

Python version 2.7 returns a list.

Python versions 3.x return a map object. To display the contents, type:

py.list(py.map(@py.len,days))
ans = 

  Python list with no properties.

    [6, 7, 9, 8, 6]

External Websites