Let's say I have the following data structure:
SOME_LIST = [
{
'name': 'foo',
'alternatives': [
{
'name': 'Foo'
},
{
'name': 'foo'
},
{
'name': 'fu
}
]
},
{
'name': 'bar',
'alternatives': [
{
'name': 'Bar'
},
{
'name': 'bar'
},
{
'name': 'ba'
}
]
}
]
I want to produce a flattened list of the alternative "names" of the object as follows:
['foo', 'Foo', 'fu', ..., 'ba']
I've gone around the houses with various list comprehensions...and I just don't know how to do this elegantly.
I've tried:
[i['alternatives'] for i in SOME_LIST]
[*i['alternatives'] for i in SOME_LIST]
>>>SyntaxError: iterable unpacking cannot be used in comprehension
[alt['name'] for alt in [i['alternatives'] for i in SOME_LIST]]
>>>TypeError: list indices must be integers or slices, not str