I tried the code below: the goal is to generate a dictionary where each key has a list as a value. The first iteration goes well and generates the element as I want, but the second loop, the nested loop, does not generate the list as expected.
Please help me with this simple code. There should be something wrong with it, the code looks like this:
schop = [1, 3, 1, 5, 6, 2, 1, 4, 3, 5, 6, 6, 2, 2, 3, 4, 4, 5] mop = [1, 1, 2, 1, 1, 1, 3, 1, 2, 2, 2, 3, 2, 3, 3, 2, 3, 3] mlist = ["1","2","3"] wmlist=zip(mop,schop) title ={} for m in mlist: m = int(m) k=[] for a,b in wmlist: if a == m: k.append(b) title[m]=k print(title)
The results are as follows:
title: {1: [1, 3, 5, 6, 2, 4], 2: [], 3: []}
Why does the second key and third key have an empty list?
Thanks!