1

I have a list of dictionaries with info in such format:

p_mat_list = [
 {'name': 'Mirror', 'link': '/somelink1/'},
 {'name': 'Gold Leaf', 'link': '/somelink2/'}
]

First, create python list with all name values:

product_materials = []
for material in p_mat_list:
 product_materials.append(material['name'])

However, I don't know what would be best way to get all name values when list will have nested list(s) with dictionaries alongside, as below:

p_mat_list = [
 [{'name': 'Painted'}, {'name': 'Wood'}],
 {'name': 'Mirror'},
 {'name': 'Gold Leaf'}
]

How can I get all these name values: Painted, Wood, Mirror, Gold Leaf?

Also, how could I approach merging name values from all dictionaries inside every nested list into one value and then get that into list with others, so would get such values: Painted Wood, Mirror, Gold Leaf.

There won't be lists nested more levels and no more than two dictionaries inside them, so for e.g. from this list below would need to get such values: Painted Wood, Mirror, Varnished Wood, Gold Leaf.

p_mat_list = [
[{'name': 'Painted'}, {'name': 'Wood'}],
{'name': 'Mirror'},
[{'name': 'Varnished'}, {'name': 'Wood'}],
{'name': 'Gold Leaf'}
]
David C.
2,0022 gold badges22 silver badges29 bronze badges
asked Feb 8, 2017 at 0:19
1
  • Something I just noticed: should the results of your last example really include 'Painted Wood' as a single string, instead of 'Painted' and 'Wood' as separate strings? If so, it's inconsistent with your first example, and in that case I think we would need some more clarification on what you mean. Commented Feb 8, 2017 at 1:06

2 Answers 2

3

If you're open to bringing in an external library, you could use the collapse() function from more-itertools. Use it something like this:

import more_itertools as mt
list(mt.collapse(p_mat_list, base_type=dict))
 [{'name': 'Painted'},
 {'name': 'Wood'},
 {'name': 'Mirror'},
 {'name': 'Gold Leaf'}]

Then in your case, you can just extract the value corresponding to 'name' from each dict, instead of making a list of them.

>>> [d['name'] for d in mt.collapse(p_mat_list, base_type=dict)]

This has the advantage that you don't have to worry about how many levels of list nesting there are.

Rahul Agarwal
4,1187 gold badges33 silver badges56 bronze badges
answered Feb 8, 2017 at 0:32
1

The best way would be to flatten the complex list you have a function like this :

def flatten(x):
 if isinstance(x, dict) :
 return [x]
 elif isinstance(x, collections.Iterable) :
 return [a for i in x for a in flatten(i)]
 else:
 return [x]

This function, takes your p_mat_list as an argument and returns a single list of dictionaries.

get_list = flatten(p_mat_list)
product_materials = []
for material in get_list :
 product_materials.append(material['name'])

Your product_materials list :

['Painted', 'Wood', 'Mirror', 'Gold Leaf']
answered Feb 8, 2017 at 0:25
3
  • 2
    Note that some consider it an antipattern to bind a lambda to a name. These sources claim that in these cases one should define a full-fledged named function. Commented Feb 8, 2017 at 0:26
  • 2
    "Some" being the official Python style-guide. Commented Feb 8, 2017 at 0:27
  • @AndrasDeak How about now ? Commented Feb 8, 2017 at 0:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.