[Tutor] to place all zeros of an existing list in the starting of a new list
Manprit Singh
manpritsinghece at gmail.com
Sun May 30 19:56:23 EDT 2021
Dear sir ,
Consider a list ls = [2, 5, 0, 9, 0, 8], now i have to generate a new list
from this existing list , new list must contain all zeros in the starting,
all other elements of the existing list must be placed after zeros in new
list . So new list must be :
new = [0, 0, 2, 5, 9, 8]
The way I am doing it is :
>>> ls = [2, 5, 0, 9, 0, 8]
>>> sorted(ls, key=lambda x: x != 0)
[0, 0, 2, 5, 9, 8]
>>>
Is this the correct way ? or is there something better that can be done ?
Regards
Manprit Singh
More information about the Tutor
mailing list