Looking at the expected output, this seems to be what you're trying to do.
for key, value in zip(indivlist, indivIDlist):
grosdict[key].append(value)
print grosdict
#{0: [0, 1, 3], 1: [2, 4, 5, 6], 2: [7, 8, 9, 10], 3: [11, 12]}
the zip function. groups up the elements of the two list's together like this:
[(0, 0), (0, 1), (1, 2), (0, 3), (1, 4), (1, 5), (1, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 11), (3, 12)]
and then looping trough this list we have the value, and the corresponding key, to insert them into the dictionary.
Taking a look at the original code. There are some problems there.
- Same variable name in both the two nested loop.
- Altough you're not using these variables, you probably should
- You're resetting the variable x to zero at the start of every loop.
- You're trying to index different lists, of different length, with the same variable.
- You're assigning (replacing) the matching values, instead of appending them.
This modified code gives the same result.
x = 0
for ID_indiv in indivlist:
for ID_grod in groslist:
if ID_indiv == ID_grod:
grosdict[ID_indiv].append(indivIDlist[x])
x += 1
or you could do a small edit and use enumerate:
for x, ID_indiv in enumerate(indivlist):
for ID_grod in groslist:
if ID_indiv == ID_grod:
grosdict[ID_indiv].append(indivIDlist[x])