Understanding the map function

0 votes

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

If one iterable is shorter than another it is assumed to be extended with None items.

If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation).

The iterable arguments may be a sequence or any iterable object; the result is always a list.

What role does this play in making a Cartesian product?

content = map(tuple, array)

What effect does putting a tuple anywhere in there have? I also noticed that without the map function the output is abc and with it, it's a, b, c.

I want to fully understand this function. The reference definitions is also hard to understand. Too much fancy fluff.

Jul 31, 2018 in Python by bug_seeker
• 14,960 points
16 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
map isn't particularly pythonic. I would recommend using list comprehensions instead:

map(f, iterable)

is basically equivalent to:

[f(x) for x in iterable]

map on its own can't do a Cartesian product, because the length of its output list is always the same as its input list. You can trivially do a Cartesian product with a list comprehension though:

[(a, b) for a in iterable_a for b in iterable_b]

The syntax is a little confusing -- that's basically equivalent to:

result = []

for a in iterable_a:

  for b in iterable_b:

    result.append((a, b))
answered Jul 31, 2018 by Priyaj
• 56,100 points

Related Questions In Python

0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
36 views
+2 votes
5 answers

What exactly is the function of random.seed() in python?

The seed method is used to initialize the ...READ MORE

answered Oct 29, 2018 in Python by Rahul
5,056 views
0 votes
0 answers

what is the purpose of the argument 'end' in the print function?

is it necessary to use this argument ...READ MORE

May 9 in Python by Waseem
• 1,870 points
3 views
0 votes
0 answers

How can I use the power function in python 2.7?

Can you show an example? READ MORE

4 days ago in Python by Waseem
• 1,870 points
4 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points
18 views
+1 vote
1 answer

What is the function for Factorial in Python

Easiest way: math.factorial(x) (available in 2.6 and ...READ MORE

answered Aug 21, 2018 in Python by Priyaj
• 56,100 points

edited Aug 21, 2018 by Omkar 53 views
0 votes
1 answer

Understanding repr( ) function in Python

>>> x = 'foo' >>> x 'foo' So the name x is ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 56,100 points
131 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.