Home > Software design >  Python map method
Python map method

Time:08-06

I have a list my_list = [{"index": 1}, {"index": 2}]

and I want to build a new one so that the result will look like :

[
{"values":{"index": 1}, "key": "Dummy"},
{"values":{"index": 2}, "key": "Dummy"}
]

this loop way is working :

res = []
for a in my_list:
    obj = {}
    obj["values"] = a
    obj["key"] = "Dummy"
    res.append(obj)

but I wanted to know if there is a way to re write it using pyhton map method ?

CodePudding user response:

print(list(map(lambda x: {"values": x, "key": "Dummy"}, my_list)))
  • Related