+ 2
List into Dictionaries of a list.
Hello, I need help on how to convert this two list into Dictionaries inside a list. Example list_1 = ['man', 'son', 'saint', 'sink'] lis_2 = ['jeep', 'girl', 'bad', 'drawn'] This is how I want the answer to be list_dict = [{'list_1': 'man', 'list_2': 'jeep}, {'list_1': 'son', 'list_2': 'girl'}, {'list_1': 'saint', 'list_2': 'bad'}, {'list_1': 'sink', 'list_2': 'drwan'}] please who can help me turn the two list into dictionaries inside a list. Thanks.
4 ответов
+ 5
nice one Jan Markus
that's wayyyy more pythonic than my solution xD
in think that also you can make this switch to shorten it a bit
z = [(b,c) for (b,c) in zip(list_1,list_2)]
to
z = list(zip(list_1,list_2))
or a one liner solution by omitting 'z' creation
a = [{"list_1": x[0], "list_2": x[1]} for x in zip(list_1,list_2)]
*no need to cast into list actually 😅
it was only done for debugging puropses to be able to see the content
+ 7
most straight forward way
it can be done in a more "pythonic" way
https://code.sololearn.com/cc55A32Eq6bm/?ref=app
+ 6
thanks for best answer Emmanuel Chidubem
i'd go with Jan's solution tho
it's more elegant :]
+ 2
Yes Burey , Jan Markus anwser is more elegant. but can't reuse it on the 'go', you will need to redefine, which I don't find funny.
But your own comes in as a function, which one can use and assign lists and variables on the 'go'
Anyway the good thing is that everybody solved it but with different anwser.
kudos for you guys.