How to create a list element which counts as a element but it should not have any value and while sorting the list it should be at last
Question:
How to create a list element which counts as element but dont have any value for its own, and also if we wanna sort that list that no value elements should go to last
How to create a list like this [1,2,_]
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
for i in range(0,len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j]:
nums[i] = None
nums.sort(key=lambda e: (e is None, e))
break
return set(nums)
Answers:
It is simpler without looping:
res = [i if i not in nums[:n] else None for n, i in enumerate(nums)]
res.sort(key=lambda e: (e is None, e))
return res
How to create a list element which counts as element but dont have any value for its own, and also if we wanna sort that list that no value elements should go to last
How to create a list like this [1,2,_]
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
for i in range(0,len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j]:
nums[i] = None
nums.sort(key=lambda e: (e is None, e))
break
return set(nums)
It is simpler without looping:
res = [i if i not in nums[:n] else None for n, i in enumerate(nums)]
res.sort(key=lambda e: (e is None, e))
return res