How to Check if Two Lists are Equal in Python
In this tutorial, we will learn how to program "How to Check if Two Lists are Equal in Python." The objective is to check if two lists are equal. This tutorial will guide you step by step through the process of determining whether two lists are equal. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.
This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of checking if two lists are equal. So, let's dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class LinkedList:
- def __init__(self):
- self.head = None
- self.last_node = None
- def append(self, data):
- if self.last_node is None:
- self.head = Node(data)
- self.last_node = self.head
- else:
- self.last_node.next = Node(data)
- self.last_node = self.last_node.next
- def is_equal(llist1, llist2):
- current1 = llist1.head
- current2 = llist2.head
- while current1 and current2:
- if current1.data != current2.data:
- return False
- current1 = current1.next
- current2 = current2.next
- return current1 is None and current2 is None
- while True:
- print("\n============= Check if Two Lists are Equal =============\n")
- llist1 = LinkedList()
- llist2 = LinkedList()
- data_list = input('Please enter the elements in the first linked list: ').split()
- for data in data_list:
- llist1.append(int(data))
- data_list = input('Please enter the elements in the second linked list: ').split()
- for data in data_list:
- llist2.append(int(data))
- if is_equal(llist1, llist2):
- print('The two linked lists are the same.')
- else:
- print('The two linked lists are not the same.')
- # Ask user if they want to try again
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == 'no':
- print("Exiting program...")
- break
- elif opt != 'yes':
- print("Invalid choice. Exiting program...")
- break
This program compares two singly linked lists to check if they are identical. Users input elements for both lists, and the program traverses them node by node, comparing data values. If all corresponding nodes match and both lists are of the same length, it reports that the lists are the same; otherwise, it indicates they are different. Users can repeat the process or exit.
Output:
There you have it we successfully created How to Check if Two Lists are Equal in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language