0
why does this code give: [2, 3, 5, 56, 7, 8, 4]
https://code.sololearn.com/c0HoLCAmaTFR/?ref=app I would like that if there are two identical values in the list, then it removes them. But it turns out that the code only deletes the first value and the second does not I want to output: [2, 3, 5, 56, 7, 8] When you enter 4
5 odpowiedzi
+ 1
Rostislav Khalilov
remove() function will remove only 1st occurance.
You can do like this:
x1 = 4
x = [2, 3, 4, 5, 56, 7, 8, 4]
y = []
[y.append(i) for i in x if i != x1]
print (y)
0
🅰🅹 🅐🅝🅐🅝🅣
or you can do it with the remove () function?
0
Rostislav Khalilov
You can see explanation here:
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/list/remove
0
🅰🅹 🅐🅝🅐🅝🅣
but how to swap the largest element of the list with the smallest?
0
Rostislav Khalilov
Do you want to swap or sort elements?
If you want to sort then do this
y.sort()
print (y)