Jun-24-2020, 03:37 AM
If lst has less elements than n, sum returns the sum of the entire list.
def sum_first_n_nums(lst, n): return sum(lst[:n])And since sum fixes things automatically there isn't any reason to write you own function.