官术网_书友最值得收藏!

Map, Lambda, zip, and filters

In this section, we are going to understand some very handy Python functions. These allow us to carry out quick processing operations on Python iterables such as lists.

  • Map(): As we saw earlier, list comprehensions come in very handy when we have to perform an operation on all or some of the elements in a list. The same can be achieved with the help of the map function. This takes two arguments, the first being the function that will perform the manipulation on the elements of the list, and the second being the list itself. The following example, map_usage.py, demonstrates this:
  • Lambda(): Lambda functions are small but powerful in-line functions that can be used for manipulation of data. They can be very useful for small manipulations, as very little code is required to implement them. Let's look at the same example again, but this time we will use a Lambda function in place of a normal Python function:
  • Zip(): The zip method takes two lists or iterables and aggregates the elements across multiple iterables. Finally, it returns us a tuple iterator that contains the aggregation. Let's make use of a simple code, zip_.py, to demonstrate this function:
  • Filter() :The filter method is used to filter out the elements of the list that return true for a particular condition. The filter method takes two arguments, the first being the method or Lambda function that either returns true or false for a particular element, and the second being the list or iterable to which that element belongs. It returns a list that contains the elements for which the condition evaluated as true. Let's create a file called filter_usage.py, and add the following content:
主站蜘蛛池模板: 盱眙县| 白沙| 准格尔旗| 昌乐县| 庆阳市| 泸州市| 浑源县| 长汀县| 江华| 金坛市| 万山特区| 保德县| 宁安市| 巩义市| 丰顺县| 镇江市| 白城市| 赤壁市| 新乐市| 邮箱| 张家川| 阿瓦提县| 兰考县| 许昌市| 赣榆县| 宝兴县| 和顺县| 白山市| 乐至县| 兴化市| 滕州市| 中牟县| 临颍县| 广宁县| 东港市| 丽水市| 呼图壁县| 宁津县| 于田县| 塔城市| 上高县|