
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Numbers with Repeating Digits in Python
In this article, we will learn how to remove numbers with repeating digits in python.
Methods Used
The following are the various methods to accomplish this task ?
Using list comprehension and set() function
Using re module
Using the Counter() function
Example
Assume we have taken an input list containing numbers. We will now remove all the numbers from the list containing repeating digits and return the resultant list using the above-mentioned methods.
Input
inputList = [3352, 8135, 661, 7893, 99]
Output
[8135, 7893]
In the above input list, in the 1st element 3352, 3 is repeated twice. Hence it is removed. But the 8135 has no repeating digits hence it is retained. Similarly, 661, and 99 are removed as they contain repeating characters.
Therefore the output list contains only 8135, 7893 elements.
Method 1: Using list comprehension and set() function
len() ? The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string.
set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates)
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task -.
Create a variable to store to the input list and print the given list.
Traverse through numbers(elements) of the given list using list comprehension.
Convert each number to a string using the str() function(returns the string format of the object i.e converts it into a string).
Convert this number string to set using the set() function that removes the duplicate digits of the number.
Check if the length of the string(number) is equal to the length of the above set.
Print the resultant list after removing elements with repeating digits from the input list.
Example
The following program returns the resultant list after removing the numbers containing repeating digits from the input list using the list comprehension and set() function -
# input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # Traversing through the numbers of the list using list comprehension # Convering numbers to string and finding a set of strings (removes duplicates) # Checking if the length of the set is equal to the number of digits resultList = [i for i in inputList if len(set(str(i))) == len(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList)
Output
On executing, the above program will generate the following output -
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]
Time Complexity ? O(n)
Auxiliary Space ? O(n)
Method 2: Using re module
re.compile() method
A regular expression pattern can be combined into pattern objects, which can then be used for pattern matching using this re.compile() method. The method also helps to search for a pattern again without rewriting it.
Syntax
re.compile(pattern, repl, string):
re.search() function
Searches the entire target string for occurrences of the regex pattern and returns the appropriate Match Object instance where the match was found. It returns only the first match to the pattern from the target string.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Use the import keyword to import the re module (regex).
Use the compile() function of re module by giving the regex pattern to remove elements with repeating digits.
Traverse through the elements of the list and check if the list elements match the above regex pattern using the search() function.
Example
The following program returns the resultant list after removing the numbers containing repeating digits from the input list using re.complie() and re.search() functions -
# importing re module import re # input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # regex pattern to remove elements with repeating digits regexPattern = re.compile(r"(\d).*\1") # Checking list elements for the above regex pattern matches resultList = [i for i in inputList if not regexPattern.search(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList)
Output
On executing, the above program will generate the following output -
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]
Time Complexity ? O(n)
Auxiliary Space ? O(n)
Method 3: Using the Counter() function
Counter() function(a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked).
Example
The following program returns the resultant list after removing the numbers containing repeating digits from the input list using the Counter() function -
# importing a Counter function from the collections module from collections import Counter # input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # Counter gives the unique keys(digits) of the number resultList = [i for i in inputList if len(Counter(str(i))) == len(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList)
Output
On executing, the above program will generate the following output -
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]
Time Complexity ? O(n)
Auxiliary Space ? O(n)
The frequency of each digit of the number is given by the Counter() method here. It thus has the unique keys (digits) for the given number. The length of the given number was then compared to the number of unique digits returned by the counter
Conclusion
In this article, we learned 3 different methods for removing integers from a list that have repeated digits. Additionally, we learned how to find pattern matches in iterables using the regex module.