Advertisement
treyhunner

any and generator expression

Nov 10th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # Before
  2. def contains_duplicate(array):
  3.     for i in range(len(array)):
  4.         for j in range(i):
  5.             if array[i] == array[j]:
  6.                 return True
  7.     return False
  8.  
  9. # After
  10. def contains_duplicate(array):
  11.     return any(
  12.         array[i] == array[j]
  13.         for i in range(len(array))
  14.         for j in range(i)
  15.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement