Reputation: 901
I have a list of dictionaries:
some_list = [
{
item1:'a',
item2:['1', '2']
},
{
item1:'b',
item2:['1', '2']
},
{
item1:'a',
item2:['1']
}
]
I would like to get:
['a 1', 'a 2', 'b 1', 'b 2'] where each value from item 1 is paired with value from item 2 for each dictionary, and then only the unique strings are left.
I can think of an obvious way to do it, namely:
I am wondering if these is a more elegant way to do it using list comprehension.
Upvotes: 4
Views: 101
Reputation: 3706
another formatting option, runs back to 2.7
sorted(map(' '.join,
{(d['item1'], v)
for d in some_list
for v in d['item2']}))
still a 'one-liner' but with decorative line breaks and indentation
inner list comp same as other ans, arrived at independently without seeing it 1st
Upvotes: 1
Reputation: 1701
def fun(item):
return [item[item1]+' '+k for k in item[item2]]
res = []
[res.append(fun(i)) for i in some_list if(fun(i)) not in res]
print res
this should work
Upvotes: 1
Reputation: 95948
If order doesn't matter, then you can easily translate your logic into a set-comprehnsion (and convert to list
if you'd like):
In [1]: some_list = [
...: {
...: 'item1':'a',
...: 'item2':['1', '2']
...: },
...: {
...: 'item1':'b',
...: 'item2':['1', '2']
...: },
...: {
...: 'item1':'a',
...: 'item2':['1']
...: }
...: ]
In [2]: {f"{d['item1']} {v}" for d in some_list for v in d['item2']}
Out[2]: {'a 1', 'a 2', 'b 1', 'b 2'}
Upvotes: 4