-1

On this Huffman code, I found the below code.

heap = [[wt, [sym, ""]] for sym, wt in symb2freq.items()]

Is that possible to convert this kind of list compression into a for-loop?

shantanoo
  • 3,617
  • 1
  • 24
  • 37
Matthew
  • 17
  • 6

2 Answers2

0

Yes, you can:

heap = []
for sym, wt in symb2freq.items():
    heap.append([wt, [sym, ""]])
N Chauhan
  • 3,407
  • 2
  • 7
  • 21
-1

This works for me

heap = [[wt, [sym, ""]] for sym, wt in enumerate(symb2freq.items())]