List operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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:)

17th Jan 2019, 10:20 PM
clemens
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.
18th Jan 2019, 1:24 AM
Diego
Diego - avatar
+ 3
For a beginner like me, the only way I know: a.sort() a[0]=14
27th Feb 2022, 12:23 PM
ninoluna
+ 2
a[0] = 14 Sorry I don't know more
4th Jun 2022, 4:32 PM
Ramtin Jafari
Ramtin Jafari - avatar
+ 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.
18th Jan 2019, 3:00 AM
Шащи Ранжан
Шащи Ранжан - avatar
0
a[0] = 14
17th Jan 2019, 10:35 PM
Hubert Dudek
Hubert Dudek - avatar
0
This list operations are like hell for me. It’s sooo confusing 🙈
21st Nov 2022, 7:23 PM
Jakub Huťa
Jakub Huťa - avatar
- 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
1st Jul 2021, 8:49 AM
Manepalli Prabhu Teja
Manepalli Prabhu Teja - avatar