I am trying to solve the following problem:
I have a pretty long list of integer numbers in a given range, most of them contain numbers with repeated digits like in the example below.
[123456, 889756, 854123, 997886, 634178]
My goal is to remove the ones with repeated digits or get a new list with numbers having only distinct digits:
[123456, 854123, 634178]
Is there a nice way how to do this? Thank you very much in advance!
You can use
set on the string representation of the number to see how many digits make it into the set. If that is the same number of digits as the original number has, it passes the test:
lst = [123456, 889756, 854123, 997886, 634178] result = [n for n in lst if len(set(str(n))) == len(str(n))] print(result)
As commented below, benchmarks confirm that it is advantageous to perform an inline assignment to a temporary variable:
result = [n for n in lst if len(set(s := str(n))) == len(s)]