user9720412
user9720412

Reputation:

python: loop over dict with multiple values per key

i have the following dict:

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}

as seen, the "LocationId_Name" key has two values seperated by commas. what i want to do is, loop over the "LocationId_Name" key and create two corresponding dicts the i can hand over to a api call. output should be like this.

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}
{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}

i tried the following but this just prints out garbage...

d_list = []
keys = ['Name', 'LocationId_Name', 'NodeId_Name']
add_data = {x:data_container[x] for x in keys}
print(add_data)
for key, values in add_data.iteritems():
    for value in values:
        d_list.append([key, value])
print(d_list)

[['NodeId_Name', 'l'], ['NodeId_Name', 'v'], ['NodeId_Name', 'g'], ['NodeId_Name', 'w'], ['NodeId_Name', 'a'], ['NodeId_Name', 't'], ['NodeId_Name', 'c'], ['NodeId_Name', 'h'], ['NodeId_Name', 'i'], ['NodeId_Name', 't'], ['NodeId_Name', '0'], ['NodeId_Name', '1'], ['NodeId_Name', 't'], ['LocationId_Name', 'l'], ['LocationId_Name', 'o'], ['LocationId_Name', 'c'], ['LocationId_Name', '_'], ['LocationId_Name', 'g'], ['LocationId_Name', 'r'], ['LocationId_Name', 'u'], ['LocationId_Name', ','], ['LocationId_Name', 'l'], ['LocationId_Name', 'o'], ['LocationId_Name', 'c'], ['LocationId_Name', '_'], ['LocationId_Name', 'v'], ['LocationId_Name', 'i'], ['LocationId_Name', 'r'], ['LocationId_Name', 't'], ['LocationId_Name', 'u'], ['LocationId_Name', 'a'], ['LocationId_Name', 'l'], ['LocationId_Name', '_'], ['LocationId_Name', 'v'], ['LocationId_Name', 'i'], ['LocationId_Name', 'e'], ['Name', 'a'], ['Name', 'i'], ['Name', 'x'], ['Name', 'b'], ['Name', 'u'], ['Name', 'i'], ['Name', 'l'], ['Name', 'd'], ['Name', 'h'], ['Name', 'o'], ['Name', 's'], ['Name', 't']]

background is, that this dumb api is unable to process the two values in one call.

Upvotes: 2

Views: 7341

Answers (7)

Harish
Harish

Reputation: 157

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}

code

for i in range(len(a["LocationId_Name"].split(","))):
    b = a.copy()
    b["LocationId_Name"] = b["LocationId_Name"].split(",")[i]
    print(b)

resutls

{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}
{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}

Upvotes: 0

Sunitha
Sunitha

Reputation: 12005

If you are using python 3.5 or above

d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
d2 = [{**d, k:sv} for k,v in d.items() for sv in v.split(',') if ',' in v]
pprint(d2)

Output:

[{'LocationId_Name': 'loc_gru',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'},
 {'LocationId_Name': 'loc_virtual_vie',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'}]

Upvotes: 0

Jérôme
Jérôme

Reputation: 14674

Here's a verbose step-by-step solution:

my_dict = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
# pop removes the item from the dict, split separates the string on the commas
locations = my_dict.pop('LocationId_Name').split(',')
print(locations)
# ['loc_gru', 'loc_virtual_vie']
output_dicts = []
for loc in locations:
    new_dict = {'LocationId_Name': loc}
    new_dict.update(my_dict)
    output_dicts.append(new_dict)
print(output_dicts)
# [{'LocationId_Name': 'loc_gru', 'NodeId_Name': 'lvgwatchit01t', 'Name': 'aixbuildhost'}, {'LocationId_Name': 'loc_virtual_vie', 'NodeId_Name': 'lvgwatchit01t', 'Name': 'aixbuildhost'}]

Upvotes: 1

jc1850
jc1850

Reputation: 1086

Use values.split(',') before you iterate over it, this will turn it into a list.

Upvotes: -1

timgeb
timgeb

Reputation: 78650

Assuming you are using Python3.5 or newer:

In [1]: d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
In [2]: k = 'LocationId_Name'
In [3]: vals = d[k].split(',')
In [4]: 
In [5]: dicts = [{**d, k:v} for v in vals]
In [6]: dicts
Out[6]: 
[{'LocationId_Name': 'loc_gru',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'},
 {'LocationId_Name': 'loc_virtual_vie',
  'Name': 'aixbuildhost',
  'NodeId_Name': 'lvgwatchit01t'}]

Upvotes: 2

Rakesh
Rakesh

Reputation: 82765

Using a simple iteration and dict

Ex:

d = {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru,loc_virtual_vie', 'Name': 'aixbuildhost'}
res = []
for i in d["LocationId_Name"].split(","):
    newVal = dict((j, i) if j == "LocationId_Name" else (j, d[j]) for j in d.keys()) 
    res.append(newVal)
print(res)

Or a one-liner

print( [dict((j, i) if j == "LocationId_Name" else (j, d[j]) for j in d.keys()) for i in d["LocationId_Name"].split(",")] )

Output:

[{'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_gru', 'Name': 'aixbuildhost'}, {'NodeId_Name': 'lvgwatchit01t', 'LocationId_Name': 'loc_virtual_vie', 'Name': 'aixbuildhost'}]

Upvotes: 0

jedwards
jedwards

Reputation: 30210

Really close!

This line:

for value in values:

...when values is a string (which it is), will iterate over the characters in the string, which is why you get the output you do.

If you were to change that to something like:

for value in values.split(','):

...you'd get what you're looking for.

Here, we just call the .split() method on the values string, the results of which (in your case, a 2-element list) will be iterated over.

Upvotes: 2

Related Questions