Thanks to visit codestin.com
Credit goes to www.tutorialsteacher.com

Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python - map() Function

The map() function applies the specified function to every item of the passed iterable, yields the results, and returns an iterator.

Syntax:

map(function, iterables) --> map object

Parameters:

  1. function: The function to be called for each element of the specified iterable.
  2. iterables: One or more iterables separated by a comma (such as string, list, tuple, dictionary).

Return Value:

Returns an iterator object of the map class.

Consider the following simple square function.

def square(x):
    return x*x

Now, we can call the map function with the list of numbers to get the list of results, as shown below.

Example:
>>> numbers=[1, 2, 3, 4, 5] >>> sqrs_of_numbers=map(square, numbers) >>> next(sqrs_of_numbers) 1 >>> next(sqrs_of_numbers) 4 >>> next(sqrs_of_numbers) 9 >>> next(sqrs_of_numbers) 16 >>> next(sqrs_of_numbers) 25

In the above example, the map() function applies to each element in the numbers list. This will return an object of the map class, which is an iterator, and so, we can use the next() function to traverse the list.

Map with Lambda Expression

The map() function passes each element in the list to the built-in functions, a lambda function, or a user-defined function, and returns the mapped object. The following map() is used with the lambda function.

Example: map() with lambda function
>>> sqrs_of_numbers = map(lambda x: x*x, [1, 2, 3, 4]) >>> next(sqrs_of_numbers) 1 >>> next(sqrs_of_numbers) 4 >>> next(sqrs_of_numbers) 9 >>> next(sqrs_of_numbers) 16 >>> next(sqrs_of_numbers) 25

Map with Built-in Function

In the following example, a built-in function pow() is given to map two list objects, one for each base and index parameter. The result is a list containing the power of each number in bases raised to the corresponding number in the index.

Example: map() with built-in function
>>> bases=[10, 20, 30, 40, 50] >>> index=[1, 2, 3, 4, 5] >>> powers=list(map(pow, bases, index)) >>> powers [10, 400, 27000, 2560000, 312500000]
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.