Home > Net >  Is is possible to create a dictionary having different number of values at different keys?
Is is possible to create a dictionary having different number of values at different keys?

Time:04-10

I am new to python, I have been trying this using loops, but it didn't work

import numpy as np


n  = { 1:[1,2], 2:[2,3], 3:[3,4], 4:[4,5], 5:[6,7] }

t = 2 * len(n)
k = n.keys()
straight_list = np.arange(0, t)
paired_list= []
paired_list = [(straight_list[i], straight_list[i 1]) for i in range(0, len(straight_list), 2)]

dict_obtained = dict(zip(k, paired_list))


print(dict_obtained)
    
output>>>> {1: (0, 1), 2: (2, 3), 3: (4, 5), 4: (6, 7), 5: (8, 9)}

How to create this kinda dictionary in python.

dict_desired = { 1:[0,1], 2:[2,3], 3:[4,5,6], 4:[6,7,8] }

CodePudding user response:

What you have written should work in python. Here the key-value pairs for your dictionary are Dict{int: List[int]}.

CodePudding user response:

i think below code will solve your doubt.

dict ={}
n=5

dict[1]=[0,1]
dict[2]=[2,3]
dict[3]=[4,5,6]
dict[4]=[6,7,8]

print(dict)
  • Related