Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension: adding unique elements into list?

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

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

unique_items = []
unique_items = [item for d in data for item in d['items'] if item not in unique_items]

vs. the longer form which works:

unique_items = []
for d in data:
    for item in d['items']:
        if (item not in unique_items):
            unique_items.append(item)

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.

Here's the list of dictionaries:

[{"items":["apple", "banana"]}, {"items":["banana", "strawberry"]}, {"items":["blueberry", "kiwi", "apple"]}]

output should be ["apple", "banana", "strawberry", "blueberry", "kiwi"]

I noticed someone asking a similar question on another post: Python list comprehension, with unique items, but I was wondering if there's another way to do it without OrderedDict or if that's the best way

like image 979
user3226932 Avatar asked Mar 24 '26 23:03

user3226932


1 Answers

all_items isn't continuously overwritten during the list comprehension, so you're constantly looking for things in an empty list.

I would do this instead:

data = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 1, 2, 3, 4,]

items = []
_ = [items.append(d) for d in data if d not in items]
print(items)

and I get:

[1, 2, 3, 4, 5, 6]

But there are more efficient ways to do this anyway.

like image 153
Paul H Avatar answered Mar 27 '26 13:03

Paul H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!