I have these lists:
aList = ['a','d','n','e','z']
numList=[1,14,5,26,4]
Output should be:
aDict = {'a':1, 'd':4, 'e':5, 'n':14, 'z':26}
My code was:
a = aList.sort()
b = bList.sort()
#merge both into a dict
I have these lists:
aList = ['a','d','n','e','z']
numList=[1,14,5,26,4]
Output should be:
aDict = {'a':1, 'd':4, 'e':5, 'n':14, 'z':26}
My code was:
a = aList.sort()
b = bList.sort()
#merge both into a dict
Try using a dictionary comprehension:
aDict = {key:value for key, value in zip(sorted(aList), sorted(numList))}
print(aDict)
Output:
{'a': 1, 'd': 4, 'e': 5, 'n': 14, 'z': 26}
Given:
aList = ['a','d','n','e','z'] numList=[1,14,5,26,4]
First Sort the lists:aList.sort(),numList.sort()
Then zip them:
aDict = dict(zip(aList,numList))