


Python program to calculate symmetric difference between two lists
In Python, you can use lists to save multiple items in a single variable. One of the four built-in data types in Python for storing collections of data is a list; the other three are tuples, sets, and dictionaries, each with a unique purpose.
What is a list?
Square brackets are used to build lists. The most powerful tools in Python are lists, since they are not necessarily homogeneous. Data types like integers, strings, and objects can all be found in a list. Since lists are mutable, they can be changed even after they are created.
Symmetric differences in lists
The set of elements that are in LIST1 or LIST2 but not in both constitutes the symmetric difference between the two lists LIST1 and LIST2.
There are multiple ways to calculate the symmetric difference between two lists in python, we will take the most efficient way to find the same difference.
We have converted lists into sets and then done symmetric differences between these sets.
algorithm
Create List
Convert list to set
Print the symmetric difference of the converted set
Print the symmetric difference of the list
Example
In the following program, using "in" we can find the difference between two lists. The in keyword has two functions.
Determine whether a value exists in a string, list, tuple, range, etc.
Repeatedly traverse the list in a 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 create two lists and then create an empty temporary variable. We then use a for loop to iterate over the numbers in the list. The If statement checks if the number is common and then appends the element to the first created temporary variable. Then print the list with different elements.
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]
Use list comprehension
In this approach we explicitly convert the list to a set and then use the subtraction operator to remove only one from the other list. Go to Collections in Python to get other collection references. This is a similar strategy to the one we used before. The only difference is that list comprehension syntax is used instead of nested loops.
Example
The following program is comparing two lists: list_1 and list_2. It creates a set from the second list and then uses it to filter out any values that appear in both lists. The result is a new filtered list (temp_3) containing only elements from 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]
Do not use set()
In this method, elements are copied from two lists using basic combining techniques while periodically checking whether they exist in the other list.
Example
In the following program - we define a function called Difference which accepts two lists as parameters. This function creates a new list list_dif containing items that are not present in both original lists. It then prints out this new list and 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]
Use symmetric difference
Symmetrydifference ()The method returns the elements in the first or second group. Unlike shared elements of two collections, this method does not return an intersection.
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]
in conclusion
In this article, we used four different methods to calculate the symmetric difference between two lists through python. Each method has some uniqueness and a different way of approaching the task.
The above is the detailed content of Python program to calculate symmetric difference between two lists. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software