Marco
Marco

Reputation: 55

Extract value in dict

I have this dict:

data = {'name':['Andrea', 'Luca'], 'age':['14', '15']}

I write:

for key, value in data.items():
    print (key, value)

here in this my results in (print(key, value)) are:

name ['Andrea', 'Luca']
age ['14', '15']

I would like to extract this:

name = Andrea
name = Luca
age = 14
age = 15

How can I do?

Upvotes: 2

Views: 59

Answers (2)

khelwood
khelwood

Reputation: 59111

value is your list. Iterate through it and print each item.

for key, value in data.items():
    for x in value:
        print('{} = {}'.format(key, x))

Upvotes: 1

jpp
jpp

Reputation: 164693

Just add an inner for loop:

for key, value in data.items():
    for subvalue in value:
        print(key, subvalue)

name Andrea
name Luca
age 14
age 15

Upvotes: 3

Related Questions