Python turning multiple objects into a dictionary
<p>I've got an Object which has a name and an indefinite amount of parameters (which is also an object with a name and a value)</p>

<p>I'm pretty new to python so this gets a bit over my head
I want to get all parameters and put them into a dictionary. My code looks like this:</p>

<pre><code>data = {}

for parameter in object.parameters:
    parameter.__dict__
    data.update({parameter})
</code></pre>

<p>But parameter.__dict__ doesn't work like I thought here.
Any ideas how i can make it work?</p>

<p>EDIT: Thanks it worked by using data.update(vars(parameter))</p>

<p>By looking at your output it looks like you class structure is something like this</p>

<pre><code>class class1(object):
    def __init__(self, p1, p2):
        self.name =  p1
        self.value = p2

class class2(object):
    def __init__(self):
        self.parameters = [class1("param1", "value1"), class1("param2", "value2")]


obj = class2()

data = {}
</code></pre>

<p>So when we do  </p>

<pre><code>for parameter in obj.parameters:
    print(parameter.__dict__)
</code></pre>

<p>We get output</p>

<pre><code>{'value': 'value1', 'name': 'param1'}
{'value': 'value2', 'name': 'param2'}
</code></pre>

<p>So to get the desired output you will have to do something like</p>

<pre><code>for parameter in obj.parameters:
    data[parameter.__dict__['name']] = parameter.__dict__['value']

print(data)
</code></pre>

<p>Which will give your desired output</p>

<pre><code>{'param2': 'value2', 'param1': 'value1'}
</code></pre>

python dictionary