Concatenate values of dictionary of list with nested dictionaries

Laila Campos

I want to extract values A, C, D and E (I don't need value_B, which is a number) from the following dictionary:

{'key1': [{'key_A': 'value_A', 'key_B': value_B}, 
         {'key_A': 'value_C'},
         {'key_A': 'value_D'},
         {'key_A': 'value_E'}],
'key2': True}

Then I want to concatenate those values into a single string with a new line between each value. Like so:

string = ''
for value in values:
    string += value + '\n' 

Or maybe this:

string = ''
string = '\n'.join(values)

What I want is this:

string = 'value_A' + '\n' + 'value_C' + '\n' + 'value_D' + '\n' + 'value_E'

What's the best approach to do this? How would I go about doing it? I don't even know where to start.

Any help would be appreciated. Thank you.

sphennings

While you could do this in a one liner, I'd recommend decomposing your problem into smaller sub-problems.

my_string = '\n'.join(e['key_A'] for e in my_dict['key1'])

You have a dictionary in which one of the keys contains a list of dicts. You want to end up with a string containing the value of 'key_A' from every dict in the list of dicts separated by '\n'.

To me the obvious steps are:

  1. Select the list of dicts from 'key1' of the parent dict.
  2. Collect the values of 'key_A' from the list of dicts
  3. Join the values together in a string separated by a '\n'
# Select the list of dicts from the parent dict
my_list_of_dicts = my_dict['key1']

# Select the 'key_A' value from each dict in the list
my_values = [e['key_A'] for e in my_list_of_dicts]

# Join the values together into a string separated by the '\n'
my_string = '\n'.join(my_values)

If you don't know what key to use until runtime, and you are confident that the first key will be the desired key. You can either derive the key in advance and store it or derive it for each individual dict.

# Derive the key in advance
# Get the first key of the first dict stored in my_dict['key1']
my_key = list(my_dict['key1'][0].keys())[0]
my_string = '\n'.join(e[my_key] for e in my_dict['key1'])

# Derive the first key for every dict
my_string = '\n'.join(e[list(e.keys()[0])] for e in my_dict['key1'])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unnest dictionary with nested Dictionary or list of dictionaries

dictionary of values/lists to list of dictionaries

Nested dictionary: sum values of same key of the dictionaries nested as a value of dictionary

Convert dictionaries inside into list in nested dictionary

Created a nested dictionary from a list of dictionaries

How to change format of a nested dictionary into a list of dictionaries?

Adding item to every dictionary in a list of nested dictionaries

Python loop through nested dictionary or dictionaries in a list

Flattening nested dictionary with possible list of dictionaries in python

Combining a list and multiple dictionaries to nested dictionary - python

merge nested dictionary and list of dictionaries, python

Searching and Filtering in a list of dictionaries/nested dictionary python

Python - Create Dictionary From Nested List and Dictionaries

Swap values in dictionary which contain list of dictionaries?

How to modify the values of a dictionary from a list of dictionaries

how to print dictionary values in a list of dictionaries?

Converting a dictionary with values as list of dictionaries into pandas DataFrame

Updating dictionary values based on another list of dictionaries

Split list values inside dictionary to separate dictionaries

How to concatenate values present in the list of dictionary

update a nested dictionary with values of a list

dictionary with nested list values to single list of values

How to sort dictionaries and create a dictionaries based on the values in the list inside the dictionary

change dictionary values to dictionaries from list of dictionaries in python

Create a statistics nested dictionary from a list of nested dictionaries

Fetching values from dictionaries nested in a list?

Finding nested list with identical values in Dictionaries [Python]

How to get values from list of nested dictionaries?

Remove duplicate values from list of nested dictionaries