How to compare two lists in Python

5 Answers

0 votes
lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 3, 5, 1, 2]

if (set(lst1) == set(lst2)):
    print("Equal")
else:
    print("Not equal")
    
    
    

'''
run:

Equal

'''

 



answered Apr 17, 2021 by avibootz
0 votes
import collections

lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 3, 5, 1, 2]

if (collections.Counter(lst1) == collections.Counter(lst2)):
    print("Equal")
else:
    print("Not Equal")
     
     
     
 
'''
run:
 
Equal
 
'''

 



answered Apr 17, 2021 by avibootz
edited May 15, 2024 by avibootz
0 votes
lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 3, 5, 1, 2]
 
if (lst1 == lst2):
    print("Equal")
else:
    print("Not equal")
     
     
     
 
'''
run:
 
Not equal
 
'''

 



answered Apr 17, 2021 by avibootz
0 votes
import numpy as np
 
lst1 = [1, 2, 3, 4, 5]
lst2 = [2, 4, 3, 1, 5]
 
print((np.array(lst1) == np.array(lst2)).all())
 
 
      
  
'''
run:
  
False
  
'''

 



answered Apr 17, 2021 by avibootz
0 votes
l1 = [1, 2, 3, 4]     
l2 = [1, 2, 3, 5]     
 
    
print('l1 == l2:', l1 == l2)     
 
 
      
'''
run:
  
l1 == l2: False
 
'''

 



answered May 15, 2024 by avibootz

Related questions

1 answer 147 views
3 answers 276 views
2 answers 191 views
1 answer 131 views
1 answer 99 views
2 answers 162 views
...