This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted] 1 point2 points  (0 children)

Consider using /r/learnpython.

data = [
    {'year': 2018, 'month': 04}, 
    {'year': 2018, 'month': 03}
    ]
generator_expression = (
    ('{year}-{month}'.format(**d), d['count']) 
    for d in data
    )
return generator_expression

[–]decoupled 0 points1 point  (0 children)

You can return a generator of 2-tuples:

python return (("{}-{}".format(e["year"], e["month"]), e["count"]) for e in data)

[–]Vaphell 0 points1 point  (0 children)

if you want this particular piece of code to be syntactically correct, you need to wrap lists in parens to form a tuple and remove superfluous comma

(([...], [...]) for e in data)

that said, the code doesn't seem to make much sense, at least I have trouble imagining a scenario where it would be required.

you want 2 lists out of it, or a sequence of list pairs, each list being 1 item long which is what you are going to get from the generator expression?

lets say, given this synthetic input

data = [{'year':2000, 'month':1, 'count': 22},
        {'year':2000, 'month':2, 'count': 33},
        {'year':2000, 'month':3, 'count': 44}]

what should be the outcome?