# Python Program to Calculate the Symmetric Difference Between Two Lists

In python, multiple items can be kept in a single variable by using lists. One of the four built-in data types in Python for storing data collections is the list; the other three are the tuple, set, and dictionary, each of which has a unique purpose.

### What is List?

Square brackets are used to build lists. The most effective tool in Python is the list because they don't necessarily have to be homogeneous. Data-Types like Integers, Strings, and Objects can all be found in one list. Because lists are mutable, changes can be made to them even after they have been created.

## Symmetric Difference in a List

The set of elements that are in either LIST1 or LIST2 but not in both makes up the symmetric difference between the two lists LIST1 and LIST2.

There are various ways of calculating the symmetric difference between two lists in python, we will be doing the most efficient ways of finding the same.

We have converted the list into the sets and then done the symmetric difference between those sets.

### Algorithm

• Create lists

• Convert lists to set

• Print the symmetric difference of the converted set

• Print the symmetric difference of the list

### Example

In the following program using the “in” we can find the difference between the two lists. The in keyword serves two functions.

• To determine whether a value is present in a string, list, tuple, range, etc.

• To repeatedly go through a list in for loop.

L_1 = [11, 12, 13]
L_2 = [12, 13, 42]
L_3 = [3, 42, 5]

# converting lists to set
set_1 = set(L_1)
set_2 = set(L_2)

# now print the symmetric difference when

# when the converted set is passed as a parameter
print(set_1.symmetric_difference(set_2))

# now print the symmetric difference when list is

# passed as a parameter by converting it to a set
print(set_2.symmetric_difference(L_3))


### Output

{42, 11}
{13, 3, 12, 5}


### Example

Here, we have made two list, then created a empty temporary variable. Then we have used for loop to iterate through the numbers of the list. If statement checks if the numbers are common and then the elements are appended to the first made temporary variable. Then the list with different elements is printed.

list_1 = [1, 35, 20, 25, 70, 35, 80]
list_2 = [25, 80, 35]
temp_3 = []
for element in list_1:
if element not in list_2:
temp_3.append(element)
print(temp_3)


### Output

[1, 20, 70]


## Using List Comprehension

In this approach, we explicitly turn the lists into sets and then use the subtract operator to only remove one from the other. Go to Sets in Python for other set references. It is a similar strategy to the one we previously employed. The list comprehension syntax has been used in place of the nested loops as the only difference.

### Example

The following program is comparing two lists, list_1 and list_2. It creates a set from the second list and then uses this to filter out any values that appear in both lists. The result is a new filtered list (temp_3) which contains only the elements of list_1 that are not present in SET_1 (list_2).

list_1 = [13, 15, 22, 25, 30, 54, 40]
list_2 = [54, 45, 30, 13]
SET_1 = set(list_2)
temp_3 = [x for x in list_1 if x not in SET_1]
print(temp_3)


### Output

[15, 22, 25, 40]


## Without the use of set()

In this method, elements are copied from both lists using the fundamental combination technique while being periodically checked to see if they are present in the other list.

### Example

In the following program - we define a function called Difference that takes two lists as parameters. The function creates a new list, list_dif, which contains the items from both of the original lists that are not present in both of them. Then it prints out this new list with the difference between the two original lists. In this example, it prints [1, 12, 25, 22, 30, 20].

# this method helps to get difference of two lists

# by not using set()
def Difference(list1, list2):
list_dif = [i for i in list_1 + list_2 if i not in list_1 or i not in list_2]
return list_dif
list_1 = [1, 12, 25, 22, 30, 65, 70]
list_2 = [65, 20, 70]
list_3 = Difference(list_1, list_2)
print(list_3)


### Output

[1, 12, 25, 22, 30, 20]


## Using Symmetric Difference

The symmetric difference () method returns the elements that are either in the first set or the second set. This method does not return the intersection, unlike the shared elements of the two sets.

### Example

list_1 = [23, 18, 45, 25, 40, 23, 40]
list_2 = [23, 40, 35]
set_difference = set(list_1).symmetric_difference(set(list_2))
temp_3 = list(set_difference)
print(temp_3)


### Output

[35, 45, 18, 25]


## Conclusion

In this article, we have used four different ways of calculating the symmetric difference between two lists by python. Each method carries some uniqueness and different way of handling the task.

Updated on: 24-Apr-2023

225 Views 