I tried to get it this way, but it didn’t work:
item1 = ['eraser', 'pen', 'pencil', 'ruler']
item2 = ['marker', 'pen', 'sharpner', 'compass', 'eraser']
unique_item = [item for item in item1 if item in item2]
print("Unique items from the list", unique_item)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use set and subtract the intersection from the union:
>>> set_1 = set(['eraser','pen','pencil','ruler']) >>> set_2 = set(['marker','pen','sharpner','compass','eraser']) >>> list((set_1 | set_2) - (set_1 & set_2)) ['compass', 'marker', 'sharpner', 'ruler', 'pencil']
Or use method set.symmetric_difference():
>>> list(set_1.symmetric_difference(set_2)) ['compass', 'ruler', 'pencil', 'marker', 'sharpner']
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0