Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - format and mapping a list with lambda - 'list' object has no attribute 'map'

Trying to format list so i can execute it in a query but getting a 'list' object has no attribute 'map' and sure why? Using python 2.6 The list has string of names.

params = ",".join(flagged_job_names.map(lambda x: "?"))

cursor.execute(sql.format(params), flagged_job_names)
like image 694
user3590149 Avatar asked Dec 26 '22 03:12

user3590149


1 Answers

map is a function, not a method of a list.

params = ",".join(map(lambda x: "?", flagged_job_names))

BTW, you can use list comprehension or generator expression instead of map:

params = ",".join("?" for x in flagged_job_names)

But for this specific case, following are also possible:

params = ",".join(["?"] * len(flagged_job_names))

params = ",".join("?" * len(flagged_job_names))

The last one is possible, because the string (?) is one-character.

like image 124
falsetru Avatar answered Jan 14 '23 03:01

falsetru