Unable to multiply in a list

I’m trying to multiply in a list but I’m getting an error in my code.

Here is the problem:

Create a function named double_index that has two parameters: a list named lst and a single number named index . The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst . If index is not a valid index, the function should return the original list. For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:

double_index([1, 2, 3, 4], 2)

Here is my code:

#Write your function here
def double_index(lst, index):
  new_lst = lst[index]
  double_index = index * 2
  return new_lst[double_index]


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

Do I need to use slice my list in order multiply it? I feel like using the multiplication operator would suffice.

The function should return a new list

Right now, you’re returning a specific index of a list. Whereas they’re asking for a return of a list specifically.

If index is not a valid index, the function should return the original list

You need a way to check for this “base case”.

The element at index should be double the value of the element at index of the original lst

You write double_index = index * 2. But are they asking for double the value, or double the index?

Consider: You have to return a new list, but it doesn’t need to be called new_lst necessarily. If you modify the existing lst and return it, it should be good enough (given all conditions are met).

I have many questions but I’ll start with this:

If index is not a valid index, the function should return the original list

How can I determine if a something is valid within an index? My mind goes here:

index_value = lst[index]
if index != index_vlaue
return lst

That’ll throw an error if it’s out of range, and there’s no need to go into try/except for something like this.

Consider using len(lst) in tandem with index (though you have to account for 0-indexing).

1 Like

@toastedpitabread

How do I use len(lst) in tandem with index? There are too many variables in play here. Maybe I don’t understand what index is even supposed to be.

 #Write your function here
def double_index(lst, index):
  new_lst = len(lst[index - 1])
  return new_lst


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

I don’t understand how I am supposed to interact with an index variable inside a list. I keep encountering errors. How can I convert into something more tangible to work with?

You need to check if the variable index would be an invalid index for the list lst. The way this occurs is if the index is greater than the highest index in the list. (Remember list indices start a 0). As for the aim of the code, you should return the list provided, however with the element at the index index-provided it is valid-doubled. Consider the following list:

list1 = [1, 2, 3, 4, 5]

If this is the call to your function:

double_index(list1, 2)

The returned value should be:

[1, 2, 6, 4, 5]

As the list is returned (with every element but one exactly the same), where the element at the index index is doubled.
I hope this helps!

@codeneutrino

Yes it does thank you! I am having issues with doubling the index index though. I was thinking about just doubling with multiplication and adding it to a new variable.

double_index = index * 2

You need to check if the variable index would be an invalid index for the list lst . The way this occurs is if the index is greater than the highest index in the list. (Remember list indices start a 0 ).

What do you mean by this exactly? Doe you mean greater than the size of the numbers contained within the list or the actual size of the index (ex. 5 integers vs. 4 integers?)

Yes it does thank you! I am having issues with doubling the index index though. I was thinking about just doubling with multiplication and adding it to a new variable.

Except you have to double the value at the given index, not the index itself (as per the instructions.

You’re doubling just great, it’s just the wrong thing you’re doubling.

As @toastedpitabread suggested, use len()-that’s a clue. What does the len function do? Once you’ve answered that, you can then think: “how can I use it to validate the index variable”. Once you know that, you can write the code to do that.

@codeneutrino @toastedpitabread

Okay thanks all. This put me on a new path of thinking and I was able to multiple the value of the index successfully but I’m still struggling with validating the index.

#Write your function here
def double_index(lst, index):
  validate_index = len(lst)
  value = lst[index]
  value_doubled = value * 2
  if validate_index > value
  return lst


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

So when we find out the length of our list, we basically know all its indexes.

What kind of index is not valid in a given list? One that doesn’t exist… (which would be what?)

Let’s put it into a real scenario. School lockers: let’s say there’s 30 in a class. They’re 0-indexed (who knows for what reason ahhahahah)…

So I can access 0-29. Which numbers can I not access?

@toastedpitabread

Haha I believe you can’t access the last one so 30.

Maybe lst[-1]?

Thats’s correct! So, in relation to the numbers 0-29, what is 30? (Is it bigger, smaller, etc). What does that tell you about invalid indexes?

lst[-1] would actually mean the last value in lst, due to the way list indexing/slicing works in Python.

@codeneutrino

I think I see 30 is 1 + the last value so maybe in the problem I’m trying to solve it would be

#Write your function here
def double_index(lst, index):
  validate_index = len(lst)
  index_plus_one = index + 1
  value = lst[index]
  value_doubled = value * 2
  if validate_index < index_plus_one:
     return lst


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

You’re almost right! What is that number (index) was 2 greater than len(lst), or 3 greater? Should you make that part of the validation process.

Edit: The above post by @jrb0831 was mostly correct-I mis read it.

@codeneutrino

Would I use .append(1)? I’m not too sure how to validate multiple values. is there some type of loop function?

I mean, if the index value was-for our example-was 31. Would that also be incorrect. What if it was 32? On a more general sense, what is 30 in relation to 29-as in, is it bigger or smaller, etc?

For the record…

lst[-1]

is an index reference, not a slice.

lst[-1:]

is a slice.

1 Like

Yes is that what I am doing in my if statement?

using the > (greater than) symbol

#Write your function here
def double_index(lst, index):
  validate_index = len(lst)
  index_plus_one = index
  value = lst[index]
  value_doubled = value * 2
  if validate_index > index_plus_one:
     return lst
1 Like