Reputation: 1057
This is my code:
html_tags = [{'tag': 'a',
'attribs': [('class', 'anchor'),
('aria-hidden', 'true')]}]
I just can do it by one-level for-loop and one-level map as follow:
for index, tag in enumerate(html_tags):
html_tags[index]['attribs'] = map(lambda x: '@{}="{}"'.format(*x), tag['attribs'])
print html_tags
However, this is my output (result):
[{'attribs': ['@class="anchor"', '@aria-hidden="true"'], 'tag': 'a'}]
How to do two-level nested map and output the same result.
Upvotes: 1
Views: 480
Reputation: 107297
I suggest a dictionary comprehension :
>>> html_tags = [{i:map(lambda x: '@{}="{}"'.format(*x), j) if i=='attribs' else j for i,j in html_tags[0].items()}]
>>> html_tags
[{'attribs': ['@class="anchor"', '@aria-hidden="true"'], 'tag': 'a'}]
>>>
Also instead of using map
with lambda
as a more efficient way you can use a list comprehension :
>>> html_tags = [{i:['@{}="{}"'.format(*x) for x in j] if i=='attribs' else j for i,j in html_tags[0].items()}]
>>> html_tags
[{'attribs': ['@class="anchor"', '@aria-hidden="true"'], 'tag': 'a'}]
>>>
Upvotes: 2