0

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)
3
  • 1
    for i in nums / nums[i] is not the proper way to index the list nums. Commented Jun 5, 2017 at 16:33
  • 2
    i is not an index, it is the number (the list element) itself. Commented Jun 5, 2017 at 16:33
  • Not an answer to your question, but here is one way to achieve your aim: nums[:] = [max(nums[0], nums[-1])]*len(nums). Commented Jun 5, 2017 at 16:34

5 Answers 5

2

Here is an option:

the_list = [6, 7, 2, 4, 5, 7]

val = max([the_list[0], the_list[-1])

the_list = [val]*len(the_list)
Sign up to request clarification or add additional context in comments.

Comments

2

You've confused an index with a list element. Consider the input list [17, 3, -42, -3]. Your for statement steps i through the values 17, 3, -42, and -3, in that order. However, the assignments you do assume that i is taking on the position or index values, 0, 1, 2, 3.

Try this, instead:

def max_end3(nums):
    for i in range(len(nums)):
        if (nums[0] > nums[len(nums)-1]):
            nums[i] = nums[0]

Comments

1

You do not have to do for loop since 'nums is already a list. I believe coding bat is giving array length is 3. So try this

def max_end3(nums):
  if nums[0] > nums[-1]:
    return nums[:1] *3
  elif nums[-1] > nums[0]:
    return nums[-1:] * 3
  elif nums[0] == nums[-1]:
    return nums[:1] * 3
  else:
    return nums

Comments

1

for i in nums / nums[i] is not the proper way to index the list nums. I think you can try this instead:

def max_end3(nums):
    for i,num in enumerate(nums):
        if (nums[0] > nums[-1]):
            nums[i] = nums[0]
        else:
            nums[i] = nums[-1]
    return (nums)

Use the built-in enumerate function to return "a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence." This allows you to concurrently index & iterate the values.

Use nums[-1] to represent the last item in the list, since Python supports negative indexing :)

Note: other solutions are more elegant, and this is not how I would solve the problem, but for a beginner it is sometimes easier to understand if working with simple constructs like for loop.

Comments

0

To replace each element in the given list I would pre-compute the wanted value, and then assign it to each element, thus:

def max_end3(nums):
    larger = max((nums[0], nums[-1]))
    for i in range(len(nums)):
        nums[i] = larger

You could alternatively leave the original list unchanged, and generate a new list of the wanted values. If the new list is assigned to the same name as the old one, it will have a similar effect, eg.

nums = [max((nums[0], nums[-1]))] * len(nums)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.