Python list comprehension: adding unique elements into list?
<p>I'm trying to write simpler code for adding unique elements into a python list. I have a dataset that contains a list of dictionaries, and I'm trying to iterate through a list inside the dictionary</p>

<p>Why doesn't this work? It's adding all the items, including the duplicates, instead of adding unique items.</p>

<pre><code>unique_items = []
unique_items = [item for d in data for item in d['items'] if item not in unique_items]
</code></pre>

<p>vs. the longer form which works:</p>

<pre><code>unique_items = []
for d in data:
    for item in d['items']:
        if (item not in unique_items):
            unique_items.append(item)
</code></pre>

<p>Is there a way of making this work using list comprehension, or am I stuck with using double for loops? I want to keep the ordering for this.</p>

<p>Here's the list of dictionaries:</p>

<pre><code>[{"items":["apple", "banana"]}, {"items":["banana", "strawberry"]}, {"items":["blueberry", "kiwi", "apple"]}]
</code></pre>

<p>output should be ["apple", "banana", "strawberry", "blueberry", "kiwi"]</p>

<p>I noticed someone asking a similar question on another post: <a href="https://stackoverflow.com/questions/12681753/python-list-comprehension-with-unique-items">Python list comprehension, with unique items</a>, but I was wondering if there's another way to do it without OrderedDict or if that's the best way</p>