input: Write a function to optimize the provided Python code using best practices. The given function performs a linear search on an array to find the index of a specified element. If the element is found, the function returns its index; otherwise, it returns -1.
  
  def linear_search(arr, x):
      for index, item in enumerate(arr):
          if item == x:
              return index
      return -1
  
  assert linear_search([3, 4, 5, 1, 2], 5) == 2
  assert linear_search([3, 5, 10, 1, 2], 1) == 3
  assert linear_search([4, 3, 1, 2], 2) == 3
  assert linear_search([3, 5, 4, 1, 2], 10) == -1
  assert linear_search([], 5) == -1
output: def linear_search(arr, x):
      return next((index for index, item in enumerate(arr) if item == x), -1)