I have to figure out which is larger, the first or last element in a given list, and set all the other elements to be that value.
I wrote a code using a for loop, but I get an error that the list index is out of range. What is my mistake?
The code is below:
def max_end3(nums):
for i in nums:
if (nums[0] > nums[len(nums)-1]):
nums[i] = nums[0]
else:
nums[i] = nums[len(nums)-1]
return (nums)
for i in nums/nums[i]is not the proper way to index the listnums.iis not an index, it is the number (the list element) itself.nums[:] = [max(nums[0], nums[-1])]*len(nums).