I am trying to choose the larger of the first and last elements of an array of len(3)
and make that all the elements of the array:
def max_end3(nums):
a = max(nums[0], nums[2])
for i in nums:
i = a
return nums
So you want to return a new list that is the maximum of the first and last element?
In Python you can access the last element with [-1]
.
so a solution that doesn't care about the length of the list(minimum one element to work) would look like this:
def set_list_to_max_of_first_last(nums):
return [max(nums[0], nums[-1])] * len(nums)
What we do here is we use []
to create a new list with one element. Then we make the array as long as the original with identical entries.
HTH