user3761151
user3761151

Reputation: 143

Union of lists in dictionary

I have such dict : (mails is list of lists with dicts inside)

result = {'id': 'no_id',
'mails': [[{'mail1': '[email protected]'},
{'mail2': '[email protected]'}],
[{'mail3': '[email protected]'},
{'mail4': '[email protected]'}],
[{'mail5': '[email protected]'},
{'mail6': '[email protected]'}]]}

how can I produce : (mails is a list of dicts)

result = {'id': 'no_id',
'mails': [{'mail1': '[email protected]'},
{'mail2': '[email protected]'},
{'mail3': '[email protected]'},
{'mail4': '[email protected]'},
{'mail5': '[email protected]'},
{'mail6': '[email protected]'}]}

Will it be good to do this?

reduce(lambda x, y: result['mails'])

Upvotes: 1

Views: 122

Answers (2)

Alex
Alex

Reputation: 1210

use chain from itertools

example:

from itertools import chain

result['mails'] = list(chain.from_iterable(result['mails']))

Upvotes: 4

sundar nataraj
sundar nataraj

Reputation: 8692

l=[]
for i in result['mails']:
    l.extend(i)

result['mails']=l[:]

Use extend to merge one list to other

in single line :print reduce(lambda x, y:x+y ,result['mails'])

given Reduce take two arguments one function and iteratable item like list

Upvotes: 1

Related Questions