+ 6
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:)
7 Answers
+ 3
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.
+ 3
For a beginner like me, the only way I know:
a.sort()
a[0]=14
+ 2
a[0] = 14
Sorry I don't know more
+ 1
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.
0
a[0] = 14
0
This list operations are like hell for me. Itâs sooo confusing đ
- 1
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