-1

I have below lines of code. I want to get the stack name in info (marked as stack). I am still in learning phase. I only want first 2 loops (for and if) and in the info I need a separate loop where I loop through dictionary whose values are in a list. I compare these values to volume id (the volume.id that I get from 1st for loop) and when I get a match, I put the key of that dictionary to stack. What I tried is below but this is not right because I am unnecessary looping and I am getting long list of repeated volumes from the first loop.
Can anyone please let me know how can I loop through the elements of list values of dictionary in one line under stack? Please let me know if I need to give more information.

for volume in volumes:
        if volume.snapshot_id == snapshot_id:
            for key,value in tempDict.items():
                for elem in value:
                    if volume.id == elem:
                       stack = key
                       info={'type':'volume',
                             'id':volume.id,
                             'display_name':volume.display_name,
                             'snapshot_id':volume.snapshot_id,
                             'created_at':volume.created_at,
                             'snapshots':[], 
                             'stack': stack}# This is where I need to work
                       vol_list.append(info)}

I also tried below for stack but I also need to add if volume.id == elem and if match then key goes in stack)

stack: {k:[elem for elem in v] for k,v in tempDict.iteritems()}}

My dictionary looks like below

{
    "deployCI2": [
        "094fd196-20f0-4e8d-b946-f74a56d2f319",
        "6a1ce382-98c6-4058-a929-95a7d2415fd0",
        "156a1409-c89d-48fe-af6a-cad12d985b7a",
        "7376f485-6f70-4e35-a16e-b3136523206b"
    ],
    "deployCI3": [
        "c8fff661-4482-4908-b722-4fac0227a8b0",
        "929cf1fa-3fa6-4f95-8464-d58e5490f4cf",
        "4c7a7755-8576-43d0-9e7a-ae7319f40f6d",
        "a8460ed5-913b-4670-a1d4-43af508c8187"
    ],
    "Lomka": [
        "17efb1f1-3bf2-44f0-97a1-ebca4b6a2a30"
    ],
    "HeenaStackXYZ": [],
    "deployCI4": [
        "9f8ffa3c-460d-43a9-8113-58e891340e1b",
        "6e535e92-4da2-4228-a6ab-c8fc8d31adcd",
        "8e26a35e-7fb9-43b3-8026-d1283f7b678c",
        "f40e5c29-b4df-4cfb-9d7f-3bcc9c4dcf9f"
    ],
    "HeenaStackXYZ-VM1-ne4rpss4bnft": [],
    "ci_a3077f8b": [
        "0956d6a3-87f4-43c3-a2f7-8a963b2d00c3"
    ]
}

What should output look like? Actually It is going to html. My major concern is getting the stack name. What I have done

1. Fetch the stack names and the ID of the volumes associated with the stack. You see that in my dictionary. 
2. I compare all the volumes in my environment and see if any volume id matches with the stack's volume ID. If yes, I need to grab that stack name and put that in the info section (That's where stack comes)
Heenashree Khandelwal
  • 659
  • 1
  • 13
  • 30

1 Answers1

0

I tried reversing the dictionary and for the stack I added - 'stack':{value for key, value in reversedDict.items() if volume.id == key}} This worked for me.

Heenashree Khandelwal
  • 659
  • 1
  • 13
  • 30