How to exclude item from the list using index

Hi all

I tried this code for the following exercise
Link to code challenge

I want to know if there is a better method to exclude an item from the list using index?

def double_index(lst, index):
  valid_index = len(lst) - 1
 # print (valid_index)
 # print (lst[index])
  if (index <= valid_index) and (index >= 0):
    new_element = 2* lst[index] 
    lst [index] = new_element
    return (lst)
  else:
    return (lst)
  

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 0))

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

1 Like

First, the function you show does not exclude anything; it doubles the element found at index. When you say “exclude”, do you mean “remove”?

The method to remove an item from a list, given its index, is lst.pop(idx)

There are a few style issues:

  • The function works OK with return, but doesn’t really need return at all (although I realize that the problem did specify a return statement.) The function modifies the list in place, and I don’t think you’ll commonly see a return statement used in a function like this one.

  • Python (unlike some other languages) understands an expression like:

if 0 <= index <= valid_index:

… which (to me) is a more readable construction than the version using and. If all carats are pointing left (<), you get a nice analogy to the number line, with the variable index just where it belongs.

  • index is actually a Python list (or string) method, and it is a good idea not to give variables the same names as methods or functions. It works often enough, to be sure, but can lead to unforseen problems. Use idx instead, just as the problem author used lst rather than list.
1 Like

Hi

Thank you for all the tips.
pop() is just the function i was looking for.

Thank you again.

Here you are setting the maximum index value, but what about the minimum? Is it necessary that the index be greater than or equal to zero?

And couldn’t we just write,

if index < len(lst)

instead of using an extraneous variable?

Back to the minimum, what is that minimum, really?

Thanks mtf. I did use a condition in if statement

but, then I like patrickd314 and your method better. They are both more logical and neat.

Hopefully will get there. Thank you for the tip. Using list length seems more prudent.:slightly_smiling_face:

1 Like

Did you figure out what the actual minimum is, yet?

I believe , that should remain 0. As index can’t be negative ( excluding the time when we try and find the element from the end of the list, that is, moving in reverse).

So, if working in default order of the list, I kept my minimum greater than equal to zero.

Please let me know if you agree.

There is no default order. We specify the order. Convention has us read lists from left to right, but the reverse is just as valid, especially for an exercise such as this where we just want to mutate one element. All that matters is the index we provide fits within the range.

To pass this lesson, we can do anything that does not raise an exception.

n = len(lst)
if -n <= index < n:

So the actual minimum is negative length.