1

I have one array like this arr1 = ['name','age','sex'] and another array that has values of this array like val1 = ['Jone','20','male'] . Now I want to make a dict that will look like this --> val = {'name':'jone','age':'20','sex':'male'}

current I am doing that dict this way -->

val = {}
val['name'] = val1[0]
val['age'] = val1[1]
val['sex'] = val1[2]

Is there any better way to do this?

njzk2
  • 38,969
  • 7
  • 69
  • 107

3 Answers3

2
>>> arr1 = ['name','age','sex']
>>> val1 = ['Jone','20','male']
>>> dict(zip(arr1, val1))
{'age': '20', 'name': 'Jone', 'sex': 'male'}
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
1

With python 2.7 onwards you could use dictionary comprehension

val = { k: v for k, v in zip(arr1, val1) }
memoselyk
  • 3,993
  • 1
  • 17
  • 28
0
val = {}
for i in range(len(arr1)):
    val[arr1[i]] = val1[i]