Home > Mobile >  Convert a list comprehension to functional programming
Convert a list comprehension to functional programming

Time:04-26

I have a list of dictionaries

lst = [{'a': (1, 2, 3), 'b': (2, 3)},
       {'c': (3, 6), 'd': (4, 8), 'e': (5, 10)},
       {'d': (6, 12), 'e': (7, 14)}]

For each key in each dictionary, I want to keep only the first element of the values. So the desired output is

[{'a': 1, 'b': 2}, {'c': 3, 'd': 4, 'e': 5}, {'d': 6, 'e': 7}]

I can get it using a list comprehension like

[{key: val[0] for key, val in dct.items()} for dct in lst]

However, I want to know if it's possible to get the same output using map, itemgetter, itertools, functools etc. What I have so far:

map(dict.values, lst)

But I don't know how to go from here.

CodePudding user response:

For nested iterations, I don't think we can do it without the help of lambda expressions:

from operator import itemgetter, methodcaller

[*map(
    lambda items: dict(zip(
            map(itemgetter(0), items),
            map(itemgetter(0), map(itemgetter(1), items))
        )), map(methodcaller('items'), lst))]
# [{'a': 1, 'b': 2}, {'c': 3, 'd': 4, 'e': 5}, {'d': 6, 'e': 7}]

I have to say it's very ugly.

  • Related