10/18 What's the correct/optimal way?


#1

def list_function(x):
r = x[1] + 3
x.insert(1, r)
del x[2]
return x

n = [3, 5, 7]
print list_function(n)

so this works, it’s just that I feel like I did a less optimal way than was intended. any advice?


#2

Hi @methodrunner79483,

You can assign a value to an element of a list by using bracket notation with an index to refer directly to that element, provided that the element already exists. There is no need to use del and insert.
An example would be …

lst[4] *= 7

Apply the same principle to the current exercise.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.