Python: print the dictionary elements which has multiple values assigned for each key
<p>I've got a dictionary like the one, below:</p>

<pre><code>{    "amplifier": ["t_audio"],
     "airbag": ["t_trigger"], 
     "trigger": ["t_sensor1", "t_sensor2"],  
     "hu": ["t_fused"], 
     "cam": ["t_front", "t_ldw", "t_left", "t_nivi", "t_rear_camera", "t_right"], 
     "video_screen": ["t_video"]    }
</code></pre>

<p>as you can see, there are some elements which have more than one value assigned for each key. I'd like to extract those values as string, separately within (preferably) a for loop then print them out. Printed result should be something like this:</p>

<pre><code>group(amplifier, t_audio)
group(airbag, t_trigger)
group(trigger, t_sensor1)
group(trigger, t_sensor2)
group(hu, t_fused)
group(cam, t_front)
group(cam, t_ldw)
...
...
</code></pre>

<p>I can easily perform this on a normal dictionary where each key has only one values but got almost confused about this one(sorry if I'm newbe to Python...). Any kind of help is appreciated on how to get this result.</p>

<p>Very simple: loop through each key in the dictionary. Since each value is going to be a list with one or more elements, just loop through those and print the string you need:</p>

<pre><code>d = {'amplifier': ['t_audio'], 'hu': ['t_fused'], 'trigger': ['t_sensor1', 't_sensor2'], 'cam': ['t_front', 't_ldw', 't_left', 't_nivi', 't_rear_camera', 't_right'], 'airbag': ['t_trigger'], 'video_screen': ['t_video']}

for key in d:
    for value in d[key]: 
        print 'group({},{})'.format(key,value)
</code></pre>

<p>You can see it in action here: <a href="https://eval.in/645071" rel="nofollow">https://eval.in/645071</a></p>

python list dictionary