user6882757
user6882757

Reputation:

How to separate the dictionary with split values

Dictionary is below

a = {'querystring': {'dataproduct.keyword': 'abc,def'}}

How to split into two dictionary with values?

a['querystring'] = {'dataproduct.keyword': 'abc,def'}

Expected out while printing

{'dataproduct.keyword': 'abc'}
{'dataproduct.keyword': 'def'}

Since dictionary is hashmap [{'dataproduct.keyword': 'abc'} {'dataproduct.keyword': 'def'}]

Disclaimer:

before executing need to check the comma

if a['querystring'] = {'dataproduct.keyword': 'abc'} then no need to split

if a['querystring'] = {'dataproduct.keyword': 'abc,def,efg'} if comma is there then only need to split

Upvotes: 1

Views: 49

Answers (2)

Mario Ishac
Mario Ishac

Reputation: 5877

A solution that works with across all top-level entries, not just the entry with key "querystring":

a = {'querystring': {'dataproduct.keyword': 'abc,def'}}
split_a = []

for value in a.values():
    for sub_key, sub_value in value.items():
        for split_sub_value in sub_value.split(","):
            split_a.append({sub_key: split_sub_value})

Resulting value of split_a is [{'dataproduct.keyword': 'abc'}, {'dataproduct.keyword': 'def'}].

Upvotes: 0

Alexander Lekontsev
Alexander Lekontsev

Reputation: 1012

[{key: item} for key, value in a['querystring'].items() for item in value.split(',')]

Upvotes: 2

Related Questions