Map Function in Python

Level 392 keystrokes

This doubles each number using map(), printing [2, 4, 6]. map() applies a function to every element in an iterable.

Python code

# Define a function that takes a number and returns its double
1def double(x):
# Multiply x by 2 and return the result
2 return x * 2
3
# Create a list of numbers
4nums = [1, 2, 3]
# map() applies the double function to each element
# list() converts the map object to a list
5result = list(map(double, nums))
# Print the transformed list
6print(result)

Output

[2, 4, 6]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 4
  • )× 4
  • ,× 3
  • =× 2
  • :× 1
  • [× 1
  • ]× 1
  • *× 1

More Python examples at level 3

← Python typing practice