Unique Elements from Both Lists

Find elements that appear in exactly one of the two lists (symmetric difference).

IntermediateTopic: List Programs
Back

Python Unique Elements from Both Lists Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to find elements unique to each of two lists

list1 = input("Enter first list elements: ").split()
list2 = input("Enter second list elements: ").split()

unique = list(set(list1) ^ set(list2))

print("Elements unique to one list:", unique)
Output
Enter first list elements: 1 2 3
Enter second list elements: 3 4 5
Elements unique to one list: ['1', '2', '4', '5']

Understanding Unique Elements from Both Lists

We use the symmetric difference operator (^) on sets to get elements present in exactly one set.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents