I am trying to fill a dictionary while using loops, problem is the loop stops after fulfilling a condition without checking if there are more available conditions to fulfill,
sec=[
[1, 2, 4.0, 100.0],
[2, 3, 1.5, 500.0],
[2, 4, 10.0, 700.0],
[2, 5, 5.75, 1500.0],
[3, 4, 11.4, 200.0],
[4, 5, 10.5, 750.0],
[4, 6, 6.75, 550.0]]
I made this list and there's also this dictionary
graph={1: [1], 2: [2], 3: [3], 4: [4], 5: [5], 6: [6]}
what I am trying to accomplish is
graph = {1: ['2'],
2: ['3', '4','5'],
3: ['4'],
4: ['5','6'],
5: [],
6: []}
how it should work is that it collects all sec[x][0] as the key of the dictionary and the sec[n][1] as the values in the dictionaries
if sec[x][0] has value 1 and sec[x][1] has the value 2 then number 2 is added to the dictionary as value for key 1
the code I got is this
def magic(numList): #turns string into int
s = ''.join(map(str, numList))
return int(s)
for i in range(1,len(loc)+1): #len of loc is 6
for n in range(0,len(loc)+1):
if magic(graph[i])==sec[n][0]:
graph[i].append(sec[n][1])
but it will only add the first value then index n will stop counting and then index i keeps going and so it will not check for more values in keys
magicfunction is doing. Also, are those supposed to be lists of strings?