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,461
  • 5
  • 31
  • 58
Leo
  • 1,787
  • 5
  • 21
  • 43

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
  • 77,341
  • 8
  • 133
  • 139
1

Try this:

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

You mean zip?

dict(zip(fees, names))
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

You want this

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

or more quite :

dict(zip(fees, names))
khelili miliana
  • 3,730
  • 2
  • 15
  • 28