0

I am using python 3.4.3 . I have list of dict with following structure

test['content'] = [
{
"time_cre" : datetimeobject,
"name": "Sam",
"num": 123
},
{
"time_cre" : datetimeobject,
"name": "Sunil",
"num": 124
}

I use map and lambda function to convert datetime object to string. My code is

map((lambda x:x.update({'time_cre': x['time_cre'].strftime("%Y-%m-%d")})),test['content'])

The above method does not reflect anything.

test['new'] = map((lambda x:x.update({'time_cre': x['time_cre'].strftime("%Y-%m-%d")})),test['content'])

The above method works but the result is

{
'content':{
"time_cre" : "2017-06-03",
"name": "Sam",
"num": 123
},
{
"time_cre" : "2017-06-03",
"name": "Sunil",
"num": 124
}
],
'new': <map object at 0x7f70e1fe4208>
}

So why its is not working in first method and why working in second method. Can anybody explain.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
Sathish Kumar
  • 547
  • 6
  • 17
  • What do you mean by "The above method does not reflect anything" and later "The above method works"? Explain that, and please also show the expected and actual outputs, plus any tracebacks. – Rory Daulton Sep 19 '17 at 10:35
  • In first method it returns same dict without anychange – Sathish Kumar Sep 19 '17 at 10:37
  • 1
    In Python3.x function like map, filter, reduct will only generate something like generator, which is not like Python2.X, you can convert the generator by list() function, to force execute the generators. – Menglong Li Sep 19 '17 at 10:40
  • `list(map((lambda x:x.update({'time_cre': x['time_cre'].strftime("%Y-%m-%d")})),test['content']))` This works fine . Thank You @MenglongLi – Sathish Kumar Sep 19 '17 at 10:49

0 Answers0