pythondictionarylambdatypeerrorpositional-argument

Using Map function with Lambda: TypeError: () takes 0 positional arguments but 1 was given


System: WIN10

IDE: MS VSCode

Language: Python version 3.7.3

Library: pandas version 1.0.1

Data source: base data supplied below

Dataset: base data supplied below

I am having an issue for some reason when trying to use the "map" function to map A converter function (I built using lambda) to iterate across a list of sample temperatures. The sample code is supplied below and it keeps throwing the following error: TypeError: () takes 0 positional arguments but 1 was given

Steps were taken:

  1. tested independent pieces of the code to ensure the list of tuples in temps made since
  2. searched online for the error code and could not find anything

Code:

temps = [('Berlin', 29), ('Cairo', 36), ('Buenos Aires', 19), ('Los Angeles', 26), ('Tokyo', 27), ('New York', 28), ('London', 22), ('Beijing', 32)]

c_to_f = lambda: (data[0], (9/5)*data[1] + 32)

list(map(c_to_f, temps))

error

TypeError: () takes 0 positional arguments but 1 was given

Solution

  • The map function will pass each element of temps as an argument to c_to_f.

    Change your c_to_f definition so it takes an argument:

    def c_to_f(data):
        return data[0], (9/5)*data[1] + 32
    

    or just do:

    list(map(lambda data: (data[0], (9/5)*data[1] + 32), temps))