+ 1
Merge 2 lists into 1 dictionary
Eg: Key=[1, 2] Value=["a", "b"] As {1:'a', 2:'b'} https://code.sololearn.com/c16cNJZvdCd1/?ref=app My first trial is
3 Réponses
+ 12
Hii Mohammad Shireen
One way is - dict(zip([1,2,3,4], [a,b,c,d]))
If you have more keys than values, and you want to fill in values for the extra keys, you can use - itertools.izip_longest.
Now see that If the lists are big you should use itertools.izip ..
---
Example -
i = iter(["a", "a", "b", "c", "b"])
j = iter([1,2,3,4,5])
k = list(zip(i, j))
for (x,y) in k:
if x in d:
d[x] = d[x] + y #or whatever your function needs to be to combine them
else:
d[x] = y
+ 5
Use builtin zip function
```
keys = [1,2]
values = ["a","b"]
print(dict(zip(keys,values)))
```
https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-zip-function/
+ 3
I didn't know about zip..
Here's my try...
https://code.sololearn.com/cViNm8tBR339/?ref=app