1

The lists have the same number of elements, and the names are unique. I wonder, how can I make a dict in one action.

This is my current code:

    fees = [fee for fee in fees]
    names = [name for name in names]
    mdict = [
        {'fees': fee[i], 'names': names[i]}
        for i, val in enumerate(fees)]
Desert Ice
  • 4,251
  • 3
  • 30
  • 57
Leo
  • 1,719
  • 5
  • 20
  • 41

4 Answers4

3

You can use zip on both lists in a list comprehension:

mdict = [{'fees': f, 'names': n} for f, n in zip(fees, names)]
Moses Koledoye
  • 74,909
  • 8
  • 119
  • 129
1

Try this:

result = dict(zip(fees, names))
okuznetsov
  • 248
  • 1
  • 5
  • 12
1

You mean zip?

dict(zip(fees, names))
RvdK
  • 19,128
  • 3
  • 59
  • 105
1

You want this

{fees[i]:y[i] for i in range(len(fees))}

or more quite :

dict(zip(fees, names))
khelili miliana
  • 3,537
  • 1
  • 14
  • 25