map function in python

# Python program to demonstrate working 
# of map. 
  
# Return double of n 
def addition(n): 
    return n + n 
  
# We double all numbers using map() 
numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
print(list(result))

4
2
Ksjdh 75 points

                                    def calculateSquare(n):
    return n*n


numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set
numbersSquare = list(result)
print(numbersSquare)

4 (2 Votes)
0
3.67
9
Hunter C 95 points

                                    # for the map function we give a function (action to act), a data action to act on that data.

# without map
def multiply_by2(li):
    new_list = []
    for number in li:
        new_list.append(number*2)
    return new_list


print(multiply_by2([1, 2, 3]))


# using map function
def multiply_by_2(item):
    return item*2


print(list(map(multiply_by_2, [1, 2, 3])))

# function that returns usernames in all uppercase letters.
usernames = ["RN_Tejas", "RN_Tejasprogrammer", "Rajendra Naik", "yooo", "Brainnnni", "Ouuuuut"]


def no_lower(item):
    return item.upper()


print(list(map(no_lower, usernames)))

# squares function
list_squares = list(range(1, 101))
print(list_squares)


def square(number):
    return number**2


square_list = list(map(square, list_squares))


def square_root(number):
    return number // number


print(list(map(square_root, square_list)))


def no_upper(string):
    return string.lower()


print(list(map(no_upper, 'TEJAS')))

3.67 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source