Search code examples
pythonpython-3.xpylint

PyLint W0108: Lambda may not be > necessary (unnecessary-lambda)


pylint is returning the below message for the code that I have below :

data.py:125:30: W0108: Lambda may not be necessary (unnecessary-lambda)

in_p = ', '.join(list(map(lambda x: "'{}'".format(x), data)))

Why is lambda not required here and how can it be refactored?


Solution

  • "'{}'".format is already a function; your lambda expression defines a function that does nothing except take an argument and pass it on to another function. You can simply write

    in_p = ', '.join(list(map("'{}'".format, data)))
    

    Some might prefer to use a list comprehension here:

    in_p = ', '.join(["'{}'".format(x) for x in data])
    

    It might also be worth using a temporary variable for readability.

    quote_it = "'{}'".format
    in_p = ', '.join(list(map(quote_it, data)))
    # in_p = ', '.join([quote_it(x) for x in data])