How would you implement binary search and explain its time complexity?
Implement binary_search(nums, target) for an ascending, sorted list of integers. Return the index of target if it exists, or -1 otherwise. If duplicate values are present, returning any matching index is valid. Use an iterative approach and explain why its time complexity is logarithmic.
Input: a sorted list nums and integer target.
Output: an integer index, or -1 when the target is absent.
def binary_search(nums, target):