Anyone know how to perform following code using list comprehension? I’m stuck due to the if condition, which I don’t know how to apply using list comprehension. Thanks in advance.
x = [{'key':1},{'key':1},{'key':2}, {'key':2}]
y = []
for e in x:
if e['key'] not in y:
y.append(e['key'])
>>>print y
[1,2]
For this particular conditional, you can use a set comprehension: