bbartling
bbartling

Reputation: 3494

Python iterate thru nested dictionary

I have a question similar to this SO post to loop thru nested dictionaries..

I have a dictionary:

vavs = {'vav_1': {'network': '10:44', 'obj': '91044'},
        'vav_2': {'network': '10:45', 'obj': '91045'},
        'vav_3': {'network': '10:46', 'obj': '91046'},
        'vav_4': {'network': '10:47', 'obj': '91047'},
        'vav_5': {'network': '10:48', 'obj': '91048'},
        'vav_6': {'network': '10:49', 'obj': '91049'},
        'vav_7': {'network': '10:50', 'obj': '91050'}} 

Is it possible to create a loop to extract the values of a network and obj on each loop iteration?

I can print vavs['vav_1']['network'] and vavs['vav_1']['obj'] manually but I am hoping to loop thru vav_1 thru vav_7 in an infinite fashion (loop doesn't end)

Upvotes: 1

Views: 3481

Answers (5)

FabioL
FabioL

Reputation: 999

If i understand what you mean for:

loop thru vav_1 thru vav_7 in an infinite fashion (loop doesn't end)

The following code print network and obj values infinite times:

while True:
    for k, v in vavs.items():
        print("{} {} {}".format(k, v['network'], v['obj']))

Upvotes: 1

rcriii
rcriii

Reputation: 707

My first thought was to use pandas:

In [155]: import pandas as pd

In [156]: vavs = {'vav_1': {'network': '10:44', 'obj': '91044'},^M
     ...:         'vav_2': {'network': '10:45', 'obj': '91045'},^M
     ...:         'vav_3': {'network': '10:46', 'obj': '91046'},^M
     ...:         'vav_4': {'network': '10:47', 'obj': '91047'},^M
     ...:         'vav_5': {'network': '10:48', 'obj': '91048'},^M
     ...:         'vav_6': {'network': '10:49', 'obj': '91049'},^M
     ...:         'vav_7': {'network': '10:50', 'obj': '91050'}}

In [157]: vdf = pd.DataFrame(vavs)

In [158]: vdf
Out[158]:
         vav_1  vav_2  vav_3  vav_4  vav_5  vav_6  vav_7
network  10:44  10:45  10:46  10:47  10:48  10:49  10:50
obj      91044  91045  91046  91047  91048  91049  91050

In [168]: x=0

In [169]: while x<1:
     ...:     for c in vdf.columns:
     ...:         print c, vdf.loc['network'][c], vdf.loc['obj'][c]
     ...:     x +=1
     ...:
vav_1 10:44 91044
vav_2 10:45 91045
vav_3 10:46 91046
vav_4 10:47 91047
vav_5 10:48 91048
vav_6 10:49 91049
vav_7 10:50 91050

Update x=0 to as many iterations as you want, and if you want to do something fancy with the network/obj pairs, pandas may have simple and fast way to do it.

Upvotes: 0

user2556381
user2556381

Reputation:

Try the following (checkout the python docs for dict.items() here):

for k, v in vavs.items():
    print(v['network'], v['obj'])


The dict.items() (or vavs.items() method in this case) creates a list of tuples where each item in the tuple is a key-value pair. Iterating over this list allows you to access each key-value pair in the dictionary.

Upvotes: 0

Steve Archer
Steve Archer

Reputation: 641

I'm not sure what expected output you're looking for, but if you just need to get the values, and you know that those two keys will be there every time, you could do something like:

for key, vav in vavs.items():
    network = vav['network']
    # do a thing with network, I guess?
    obj = vav['obj']
    # do a thing with obj, I guess?

Upvotes: 1

Poojan
Poojan

Reputation: 3519

  • This will print values for each object.
  • You can ignore value of k if not required by application.
for k, v in vavs.items():
    print(k, v['network'], v['obj'])

Or alternative

values = [x.values() for x in vavs.values()]

Upvotes: 1

Related Questions