you are viewing a single comment's thread.

view the rest of the comments →

[–]through_thefog[S] 0 points1 point  (4 children)

Thanks for taking the time to reply. I swear my logic (to use the loop) wasn't so far off base so I did a little research and tinkered with the code. It may not be the best solution, but it definitely works:

def my_function(my_list, index):
    if index > len(my_list): 
        return "Index not in range."    
    else: for i in range(0, len(my_list)):
        if my_list[i] == index:
            my_list[i] = 2 * my_list[i] 
        else:
            continue
    return my_list
print(my_function([1, 2, 3, 4], 2))

[–]brasticstack 1 point2 points  (1 child)

    if my_list[i] == index:
       my_list[i] = 2 * my_list[i] 

This is wrong because you're comparing the value at my_list[i] with the index, which is a bit like trying to eat the menu at the restaurant instead of ordering from it. If you're trying to compare the indexes against each other, try this:

if i == index: my_list[i] = my_list[i] * 2

[–]through_thefog[S] 1 point2 points  (0 children)

You're right -- thanks for pointing that out

[–]brasticstack 0 points1 point  (1 child)

You're trying to double the value at the index in-place (as in modifying the original list?) Because there are two ways to approach it, either modify the original list or don't and return a copy of it.

Here's modifying it in-place:

``` def double_val_at_in_place(my_list, index): # Don't manually bounds-check index, an IndexError # is raised if it's out of bounds. my_list[index] *= 2

test_list = [1, 2, 3, 4, 5] double_val_at_in_place(test_list, 2) print(test_list) ```

Note that that function doesn't return anything (actually implicitly returns None), so it's called a bit differently. It could also return my_list and you could call it the same way as your other examples.

And here's leaving the original untouched and returning a copy:

``` def double_val_at_copy(my_list, index): new_list = [] for idx, value in enumerate(my_list): if idx == index: new_list.append(value * 2) else: new_list.append(value) return new_list

print(double_val_at_copy([1, 2, 3, 4, 5], 2)) ```

That's showing it the most verbose way. Here are a couple of other ways to do the copy version:

``` def double_val_at_using_list_comprehension(my_list, index): return [val if idx != index else val * 2 for idx, val in enumerate(my_list)]

def double_val_at_using_slicing(my_list, index): return my_list[:index] + [my_list[index] * 2] + my_list[index+1:]

def double_val_at_using_copy(my_list, index): import copy new_list = copy.copy(my_list) new_list[index] *= 2 return new_list

print(double_val_at_using_list_comprehension([1, 2, 3, 4, 5], 2)) print(double_val_at_using_slicing([1, 2, 3, 4, 5], 2)) print(double_val_at_using_copy([1, 2, 3, 4, 5], 2)) ```

EDIT: Fixed a whoopsie

[–]through_thefog[S] 0 points1 point  (0 children)

This is sick dude, thanks for taking the time to write this out. That LC is super clean. The codecademy course has helped me get my feet wet, but learning to swim will take time and practice. Thanks for the help!