Get path of all the elements in dictionary
<p>I have a dictionary in python and want to get path of values param1 or param2, ... to replace these with some other values.  Say to get the value of param1 I need to get it like ['OptionSettings'][2]['Value']. Can I have some generic code to do this, which will print the path of all my nodes/leaves 
below is the dictionary</p>

<pre><code>{
        "ApplicationName": "Test",
        "EnvironmentName": "ABC-Nodejs",
        "CNAMEPrefix": "ABC-Neptune",
        "SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.1 running Node.js",
        "OptionSettings": [
                           {
                            "Namespace": "aws:ec2:vpc",
                            "OptionName": "AssociatePublicIpAddress",
                            "Value": "true"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:environment",
                            "OptionName": "EnvironmentType",
                            "Value": "LoadBalanced"
                            },
                           {
                            "Namespace": "aws:ec2:vpc",
                            "OptionName": "Subnets",
                            "Value": "param1"
                            },
                           {
                            "Namespace": "aws:autoscaling:launchconfiguration",
                            "OptionName": "SecurityGroups",
                            "Value": "param2"
                            },
                           {
                            "Namespace": "aws:autoscaling:asg",
                            "OptionName": "MinSize",
                            "Value": "1"
                            },
                           {
                            "Namespace": "aws:autoscaling:asg",
                            "OptionName": "MaxSize",
                            "Value": "4"
                            },
                           {
                            "Namespace": "aws:autoscaling:asg",
                            "OptionName": "Availability Zones",
                            "Value": "Any"
                            },
                           {
                            "Namespace": "aws:autoscaling:asg",
                            "OptionName": "Cooldown",
                            "Value": "360"
                            },
                           {
                            "Namespace": "aws:autoscaling:launchconfiguration",
                            "OptionName": "IamInstanceProfile",
                            "Value": "NepRole"
                            },
                           {
                            "Namespace": "aws:autoscaling:launchconfiguration",
                            "OptionName": "MonitoringInterval",
                            "Value": "5 minutes"
                            },
                           {
                            "Namespace": "aws:autoscaling:launchconfiguration",
                            "OptionName": "RootVolumeType",
                            "Value": "gp2"
                            },
                           {
                            "Namespace": "aws:autoscaling:launchconfiguration",
                            "OptionName": "RootVolumeSize",
                            "Value": "10"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:sns:topics",
                            "OptionName": "Notification Endpoint",
                            "Value": "sunil.kumar2@pb.com"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:hostmanager",
                            "OptionName": "LogPublicationControl",
                            "Value": "false"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "DeploymentPolicy",
                            "Value": "Rolling"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "BatchSizeType",
                            "Value": "Percentage"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "BatchSize",
                            "Value": "100"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "HealthCheckSuccessThreshold",
                            "Value": "Ok"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "IgnoreHealthCheck",
                            "Value": "false"
                            },
                           {
                            "Namespace": "aws:elasticbeanstalk:command",
                            "OptionName": "Timeout",
                            "Value": "600"
                            },
                           {
                            "Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
                            "OptionName": "RollingUpdateEnabled",
                            "Value": "false"
                            },
                           {
                            "Namespace": "aws:ec2:vpc",
                            "OptionName": "ELBSubnets",
                            "Value": "param3"
                            },
                           {
                            "Namespace": "aws:elb:loadbalancer",
                            "OptionName": "SecurityGroups",
                            "Value": "param4"
                            },
                           {
                            "Namespace": "aws:elb:loadbalancer",
                            "OptionName": "ManagedSecurityGroup",
                            "Value": "param4"
                            }
                           ]

}
</code></pre>

<p>This worked for me</p>

<pre><code>def dict_traverse(self,obj,path):
        if isinstance(obj, dict):
            for k,v in obj.items():
                if isinstance(v, str):
                    #if v.find('param')!=-1:
                    print(path+"["+"'"+k+"'"+"]",v)
                elif isinstance(v, list):
                    for elem in v:
                        self.myvar+=1
                        path_lst="["+"'"+k+"'"+"]"+"["+str(self.myvar)+"]"
                        self.dict_traverse(elem,path_lst)    
        elif isinstance(obj, list):
            for elem in obj:
                self.dict_traverse(elem,path)



['SolutionStackName'] 64bit Amazon Linux 2016.03 v2.1.1 running Node.js
['OptionSettings'][1]['OptionName'] AssociatePublicIpAddress
['OptionSettings'][1]['Namespace'] aws:ec2:vpc
['OptionSettings'][1]['Value'] true
['OptionSettings'][2]['OptionName'] EnvironmentType
['OptionSettings'][2]['Namespace'] aws:elasticbeanstalk:environment
['OptionSettings'][2]['Value'] LoadBalanced
['OptionSettings'][3]['OptionName'] Subnets
['OptionSettings'][3]['Namespace'] aws:ec2:vpc
['OptionSettings'][3]['Value'] param1
['OptionSettings'][4]['OptionName'] SecurityGroups
['OptionSettings'][4]['Namespace'] aws:autoscaling:launchconfiguration
['OptionSettings'][4]['Value'] param2
['OptionSettings'][5]['OptionName'] MinSize
['OptionSettings'][5]['Namespace'] aws:autoscaling:asg
['OptionSettings'][5]['Value'] 1
['OptionSettings'][6]['OptionName'] MaxSize
['OptionSettings'][6]['Namespace'] aws:autoscaling:asg
['OptionSettings'][6]['Value'] 4
['OptionSettings'][7]['OptionName'] Availability Zones
['OptionSettings'][7]['Namespace'] aws:autoscaling:asg
['OptionSettings'][7]['Value'] Any
['OptionSettings'][8]['OptionName'] Cooldown
['OptionSettings'][8]['Namespace'] aws:autoscaling:asg
['OptionSettings'][8]['Value'] 360
['OptionSettings'][9]['OptionName'] IamInstanceProfile
['OptionSettings'][9]['Namespace'] aws:autoscaling:launchconfiguration
['OptionSettings'][9]['Value'] NepRole
['OptionSettings'][10]['OptionName'] MonitoringInterval
['OptionSettings'][10]['Namespace'] aws:autoscaling:launchconfiguration
['OptionSettings'][10]['Value'] 5 minutes
['OptionSettings'][11]['OptionName'] RootVolumeType
['OptionSettings'][11]['Namespace'] aws:autoscaling:launchconfiguration
['OptionSettings'][11]['Value'] gp2
['OptionSettings'][12]['OptionName'] RootVolumeSize
['OptionSettings'][12]['Namespace'] aws:autoscaling:launchconfiguration
['OptionSettings'][12]['Value'] 10
['OptionSettings'][13]['OptionName'] Notification Endpoint
['OptionSettings'][13]['Namespace'] aws:elasticbeanstalk:sns:topics
['OptionSettings'][13]['Value'] sunil.kumar2@pb.com
['OptionSettings'][14]['OptionName'] LogPublicationControl
['OptionSettings'][14]['Namespace'] aws:elasticbeanstalk:hostmanager
['OptionSettings'][14]['Value'] false
['OptionSettings'][15]['OptionName'] DeploymentPolicy
['OptionSettings'][15]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][15]['Value'] Rolling
['OptionSettings'][16]['OptionName'] BatchSizeType
['OptionSettings'][16]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][16]['Value'] Percentage
['OptionSettings'][17]['OptionName'] BatchSize
['OptionSettings'][17]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][17]['Value'] 100
['OptionSettings'][18]['OptionName'] HealthCheckSuccessThreshold
['OptionSettings'][18]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][18]['Value'] Ok
['OptionSettings'][19]['OptionName'] IgnoreHealthCheck
['OptionSettings'][19]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][19]['Value'] false
['OptionSettings'][20]['OptionName'] Timeout
['OptionSettings'][20]['Namespace'] aws:elasticbeanstalk:command
['OptionSettings'][20]['Value'] 600
['OptionSettings'][21]['OptionName'] RollingUpdateEnabled
['OptionSettings'][21]['Namespace'] aws:autoscaling:updatepolicy:rollingupdate
['OptionSettings'][21]['Value'] false
['OptionSettings'][22]['OptionName'] ELBSubnets
['OptionSettings'][22]['Namespace'] aws:ec2:vpc
['OptionSettings'][22]['Value'] param3
['OptionSettings'][23]['OptionName'] SecurityGroups
['OptionSettings'][23]['Namespace'] aws:elb:loadbalancer
['OptionSettings'][23]['Value'] param4
['OptionSettings'][24]['OptionName'] ManagedSecurityGroup
['OptionSettings'][24]['Namespace'] aws:elb:loadbalancer
['OptionSettings'][24]['Value'] param4
['EnvironmentName'] ABC-Nodejs
['CNAMEPrefix'] ABC-Neptune
['ApplicationName'] Test
</code></pre>

python python-2.7 python-3.x dictionary boto3