Do you want to compare lists in python? Below are the basic code and extra tweaks to compare lists in python.
How to compare lists in python?
First, we will create a custom compareLists() Function.
#define the function
def compareLists(list1, list2):
result = True
for i in range(len(list1)):
if (list1[i] != list2[i]):
result = False
return result
Now Make two lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '30', '40', '20']
Then we pass the list1 and list2 to the defined function compareLists(list1,list2).
print(compareLists(list1, list2)) #function call
The below is the Full code for the above example
#define the function
def compareLists(list1, list2):
result = True
for i in range(len(list1)):
if (list1[i] != list2[i]):
result = False
return result
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '30', '40', '20']
print(compareLists(list1, list2)) #function call
Output
When you run the above program the output of the above code is True
True
Full Explanation
In the above example, we have first created a function which returns True or False after the comparison of the two lists list1 and list2. Inside the function, we first set result = True. It means this function will always return True if all the values inside the two lists are same. If any value of the list1 and list2 are different then it will return False. Here position of the values matter. If any position of value is different, then it will return False.
#define the function
def compareLists(list1, list2):
result = True
for i in range(len(list1)):
if (list1[i] != list2[i]):
result = False
return result
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
print(compareLists(list1, list2)) #function call
Output
False
Other Tweaks and Examples
1. How to return the values at the un-matched position of the list?
#define the function
def compareLists(list1, list2):
newlist = []
for i in range(len(list1)):
if (list1[i] != list2[i]):
newlist = list1[i] +","+ list2[i]
return newlist
Declare the lists
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
Call the Function
print(compareLists(list1, list2)) #function call
Full Code
#define the function
def compareLists(list1, list2):
newlist = [] #new list
for i in range(len(list1)):
if (list1[i] != list2[i]):
newlist = list1[i] +","+ list2[i]
return newlist
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
print(compareLists(list1, list2)) #function call
Output
40,30
Explanation
When you look in the function definition def compareLists(list1, list2). First, you will create a new list for storing the unmatched positions values. Then you concatenate the newlist with unmatched position values (newlist = list1[i] +”,”+ list2[i]) . When you run the above programme 40,30 will be added the newlist.
2. How to swap the values at the un-matched position of any one list?
In the first tweak, we have output the unmatched positions values. These values we have to swap in the list1. Let’s swap the values.
#define the function
def swapListsValues(list1, list2):
newList = [] #empty list
for i in range(len(list1)):
if (list1[i] != list2[i]):
#swap the list values
tempList =[]
tempList =list1[i]
list1[i] =list1[i+1]
list1[i+1]= tempList
return (list1)
Create unmatched list variables
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
Call the Function
print("list1")
print(swapListsValues(list1, list2)) #function call
print("list2")
print(list2)
The Full code of the above example is the below.
#define the function
def swapListsValues(list1, list2):
newList = [] #empty list
for i in range(len(list1)):
if (list1[i] != list2[i]):
#swap the list values
tempList =[]
tempList =list1[i]
list1[i] =list1[i+1]
list1[i+1]= tempList
return (list1)
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
print("list1")
print(swapListsValues(list1, list2)) #function call
print("list2")
print(list2)
Output
list1
['10', '40', '30', '20']
list2
['10', '40', '30', '20']
3. How to delete the values at the un-matched position from both lists?
Function Definition
#define the function
def deleteListsValues(list1, list2):
newlist = [] #new list
for i in range(len(list1)):
if (list1[i] == list2[i]):
newlist.append(list1[i])
return newlist
Create the variable
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
Call the function
print("list1")
print(deleteListsValues(list1, list2)) #function call
print("list2")
print(deleteListsValues(list1, list2)) #function call
Full code of this example
#define the function
def deleteListsValues(list1, list2):
newlist = [] #new list
for i in range(len(list1)):
if (list1[i] == list2[i]):
newlist.append(list1[i])
return newlist
#lists
list1 = ['10', '30', '40', '20']
list2 = ['10', '40', '30', '20']
print("list1")
print(deleteListsValues(list1, list2)) #function call
print("list2")
print(deleteListsValues(list1, list2)) #function call
Output
list1
['10', '20']
list2
['10', '20']
Explanation
Inside the function definition, you will first create a new list ( newlist = [ ]), a blank list for storing the final list. After that, you will iterate the loop and check for the values that are equal in both lists. In addition, when the equal values are found at the same position(index) it appends with the newlist. At last, it will return the newlist.
When you call the function, then you will get list1 and list2 with the same values at the same position.
Hope, you find this code tutorial enjoyable. If you like to add more tweaks to it, then contact us or comment below for any queries.
Also Please don’t forget to subscribe us for more code tutorial.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.