Menu Close

Compare two List in python

Write a program to compare two list and display the comparison result

Input:

  1. The Size of First List
  2. The Size of Second List
  3. First List elements
  4. Second List elements

Output:

  1. Output of comparison of List A with List B
  2. Output of comparison of List B with List A

Refer sample input and output for formatting specification.

def cmp(a,b):
  return (a>b)-(a<b)
N1=int(input("Enter the size of First list: "))
N2=int(input("Enter the size of Second list: "))
l1=[]
l2=[]
print('First List Elements: ')
for i in range(N1):
  l1.append(int(input('')))
print('Second List Elements: ')
for i in range(N2):
  l2.append(int(input('')))
print(cmp(l1,l2))
print(cmp(l2,l1))

cmp function: This function returns 1, if first list is “greater” than second list,
-1 if first list is smaller than the second list else it returns 0 if both the lists are equal.
As it returns boolean value True-1 or False-0, hence 1-0=1 and 0-1=-1
Various Inputs are given here ?


Input_1:
Enter the size of First list: 3
Enter the size of Second list: 3
First List Elements:
3
6
7
Second List Elements:
2
2
3
Output:
1
-1


Input_2:
Enter the size of First list: 2
Enter the size of Second list: 2
First List Elements:
67
99
Second List Elements:
69
91
Output:
-1
1


Morae!Q!