dictionarytuplespython-3.7linspace

Create a dictionary with multiple keys and store multiple values to it


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):...


Solution

  • 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)