-2
a = [1,2,3,4]

newarray = list(map(a,[0,*a]))

print(newarray)

output:

'list' object is not callable error

expected: Add zero to each element in array

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please **don't** tag irrelevant tags. This has nothing to do with [tag:javascript], as that clearly isn't JavaScript code (the `*a` would be a syntax error). – T.J. Crowder Mar 22 '22 at 14:13
  • By 'add 0', do you mean sum the number and 0? Or rather, do you mean prepending the string 0 to the string representations of the numbers in your list? – TheFungusAmongUs Mar 22 '22 at 14:14
  • Hi! You tried to execute some python code and got an error in a call to function `map`. The first thing to do is to check the documentation of function `map`, and make sure you called it correctly, with the appropriate parameters, and with the parameters in the correct order. The documentation is here: https://docs.python.org/3/library/functions.html#map ; as it turns out, `map` expects a function first, and a list second, not a list first and a function second. That's what the error message is telling you: "`list object not callable`" meaning it tried to use a list as if it was a function. – Stef Mar 22 '22 at 14:15
  • prepending the string 0.. Likely output - [[0,1],[0,2],[0,3],[0,4]]......... I want to achieve this using map function.. How do I use map to implement a function without referring to a function separately outside the map? – Sarfraz Nawaz Mar 22 '22 at 14:16
  • `[[0,i] for i in a]` –  Mar 22 '22 at 14:16
  • @SarfrazNawaz Using keyword `lambda`. See my answer below. – Stef Mar 22 '22 at 14:24

2 Answers2

1

Just use a list comprehension:

out = [[0,x] for x in a]

output: [[0, 1], [0, 2], [0, 3], [0, 4]]

Alternatively, itertools.repeat and zip:

from itertools import repeat
out = list(zip(repeat(0), a))
# or keep a generator
# g = zip(repeat(0), a)

output: [(0, 1), (0, 2), (0, 3), (0, 4)]

strings

as your comment is not fully clear ("prepending the string 0"), in case you really want strings, you can use:

out = [f'0{x}' for x in a]

or

out = list(map('{:02d}'.format, a))

output: ['01', '02', '03', '04']

mozway
  • 194,879
  • 13
  • 39
  • 75
0

Builtin function map expects two arguments: a function and a list.

You wrote your arguments in the wrong order: you passed list a first instead of second.

And the thing you tried to pass as a function is not really a function.

Here is a possible fix by defining a function with def:

def the_function_for_map(x):
    return [0, x]

newarray = list(map(the_function_for_map, a))

Here is a possible fix by defining a function with lambda:

newarray = list(map(lambda x: [0, x], a))

And finally, here is a possible fix using a list comprehension instead of map:

newarray = [[0, x] for x in a]

In your particular case, you can also use zip with a list full of zeroes:

newarray = list(zip([0]*len(a), a))

Or zip_longest with an empty list and a default value:

from itertools import zip_longest

newarray = list(zip_longest([], a, fillvalue=0))
Stef
  • 13,242
  • 2
  • 17
  • 28