input: Write a python function to calculate cosine similarity between two lists.
  assert abs(cosine_similarity([1, 2, 3], [4, 5, 6]) - 0.9746318461970762) < 1e-9
  assert abs(cosine_similarity([1, 0, -1], [-1, 0, 1]) - (-1.0)) < 1e-9
  assert abs(cosine_similarity([1, 2, 3], [1, 2, 3]) - 1.0) < 1e-9
output: def cosine_similarity(list1, list2):
      from math import sqrt
      dot_product = sum(a * b for a, b in zip(list1, list2))
      magnitude1 = sqrt(sum(a * a for a in list1))
      magnitude2 = sqrt(sum(b * b for b in list2))
      if magnitude1 == 0 or magnitude2 == 0:
          return 0.0
      return dot_product / (magnitude1 * magnitude2)