combine two lists in a dictionary - python -


if have 2 lists:

l_y = [1, 2]  l_z = [3, 4] 

what easiest way achieve result?:

[{'a':1, 'b':1, 'c':'y'}, {'a':2, 'b':2, 'c':'y'}... {'a':3, 'b':3, 'c':'z'}, {'a':4, 'b':4, 'c':'z'}] 

basically, if it's list l_y value of c should y, if l_z value of c should z.

i tried this:

[{'a':nb, 'b':nb, 'c':letter} nb in [l_y, l_z] letter='y' if nb l_y else 'z'] 

but got "syntaxerror"

nb returns full list instead of element anyway, don't know how this...

you use following (which doesn't require putting lists dictionary or conditional expression):

[{'a':v, 'b':v, 'c':c}      (l, c) in zip((l_y, l_z), ('y', 'z'))          v in l] 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -