- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Python Program to Remove numbers with repeating digits

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.

- Related Articles
- Python Program to Remove all digits before given Number
- Python – Remove rows with Numbers
- How to write Python Regular Expression find repeating digits in a number?
- Program to distribute repeating integers in Python
- How to generate non-repeating random numbers in Python?
- Remove K Digits in C++ program
- Program to replace all digits with characters using Python
- Write all possible 3- digit numbers (without repeating the digits) , by using the digits.(i)6,7,5 (ii) 9,0,2
- Find Numbers with Even Number of Digits in Python
- Program to count number of stepping numbers of n digits in python
- Python Program to print all distinct uncommon digits present in two given numbers
- Python program to remove row with custom list element
- How to remove characters except digits from string in Python?
- Numbers With Repeated Digits in C++
- Python program to remove rows with duplicate element in Matrix