0

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
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 4
    And what was your failed approach? Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), then edit your question accordingly. – Mr. T Mar 19 '22 at 19:57
  • Does this answer your question? [How do I convert two lists into a dictionary?](https://stackoverflow.com/questions/209840/how-do-i-convert-two-lists-into-a-dictionary) – catasaurus Mar 19 '22 at 20:08

2 Answers2

3

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}
Yılmaz Alpaslan
  • 327
  • 3
  • 16
1

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))

Prince
  • 71
  • 5