List operation | Sololearn: Learn to code for FREE!


List operation

Hey there: I have a list with two random numbers. For example a = [4,9] I want to overwrite the lower number with another number. Is there a easier way instead of: a[a.index(min(a))] = 14 Thanks for your help:)

1/17/2019 10:20:56 PM


6 Answers

New Answer


Easier or not, this is quite interesting: a[a[0]>a[1]] = 14 NOTE: This only works for lists with n=2 numbers. For n>2 use your approach.


Hubert Dudek but lower number need not to be at 0th index every time. So, clemens go with your approach, that's the easiest and the generalized way means its not limited to list of length 2 or index 0 like that.


For a beginner like me, the only way I know: a.sort() a[0]=14


a[0] = 14


a[0] = 14 Sorry I don't know more


Yeah you can in this way for i in a: if(a[0]>a[1]) min=a[0] else min=a[1] Then assign the value to min And this code works