Jinni
Jinni

Reputation: 21

Joining Key and Values in Dictionaries

I am a beginner in Python. Struggling in joining the Key and Values.

d = {'vlan 158': ['  name MARKE', '  mode vpc']}

Desired output :

vlan 158
  name MARKE
  mode vpc

I tried many things like:

print('\n'.join(['\n'.join(item) for item in d]))
print '\n'.join(d)

All these are not giving me the expected output. Any Idea ?

Upvotes: 1

Views: 59

Answers (2)

Laurent H.
Laurent H.

Reputation: 6526

I suggest you this simple solution:

d = {'vlan 158': ['  name MARKE', '  mode vpc']}

for k in d:
    print(k + '\n' + '\n'.join(d[k]))

The output is:

vlan 158
  name MARKE
  mode vpc

Upvotes: 2

Nishita
Nishita

Reputation: 924

d = {'vlan 158': ['  name MARKE', '  mode vpc']}
for key in d:
    print(key)
    print(*d.get(key), sep='\n')

You can use this to get required output. But note that operator '*' only works on python3.

Basically, if I have an array,

a=[34,45,56]

I can access the individual elements by

print(*a)

This give an output of

34 45 56

Upvotes: 2

Related Questions