Home > Net >  Create a dictionary with multiple keys and store multiple values to it
Create a dictionary with multiple keys and store multiple values to it

Time:04-07

I have a grid that has x and y coordinates. Now for each grid point I would like to assign multiple values(everything works in loop)

Suppose coordinates of x vary from 0 to 10 and y from 0 to 10(Increment of 1) and I have three values which I will read from file and assign it to specific keys.

I wanted to use dictionary for my issue and since keys are iterating variable i know that using tuples is an option so wanted to know how to proceed.

So keys and values of dictionary would look like below

(K1,K2):(V1,V2,V3)

(0,0):(1,11,21)

(0,1):(40,3,50)

(0,2):(45,2,4)

.

.

.

(4,0)..

(4,1).. .

.

.

(10,10):...

CodePudding user response:

You can create the dictionary by using two nested for loops and assign correct data for each coordinate. Something like this:

from pprint import pprint

result = {}

for x in range(11):
    for y in range(11):
        # Here you need to assign correct data for each coordinate
        result[x, y] = (1, 1, 1)

pprint(result)
  • Related