python - How to add value from list into dictionary? -


dict={} i=["abc","def","ghi","jkl"] j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]] item in i:     dict[item]=[str(j[item])] print dict 

output should

dict={"abc":["a","b","c","d"], "def":["q","w","e","r"] ...}  

how can add list dictionary in python?

use zip() function combine 2 lists:

dict(zip(i, j)) 

demo:

>>> i=["abc","def","ghi","jkl"] >>> j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]] >>> dict(zip(i, j)) {'abc': ['a', 'b', 'c', 'd'], 'ghi': ['t', 'y', 'u', 'i'], 'def': ['q', 'w', 'e', 'r']} 

zip() pairs elements lists sequence of tuples; dict() constructor takes sequence of tuples , interprets them key-value pairs.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -