Python Program to Remove Palindromic Elements from a List
Last Updated :
08 Mar, 2023
Given a list, the task here is to write Python programs that can remove all the elements which have palindromic equivalent element present in the list.
Examples:
Input : test_list = [54, 67, 12, 45, 98, 76, 9]
Output : [12, 98]
Explanation : 67 has 76 as palindromic element, both are omitted.
Input : test_list = [54, 67, 12, 45, 98, 76, 9, 89]
Output : [12]
Explanation : 98 has 89 as palindromic element, both are omitted.
Method 1 : Using str(), list comprehension and int()
In this, elements are first converted to string using str(), reversed and reconverted to integer using int() and occurrence of palindromes is checked, if present, both the element and its palindrome are omitted from result.
Example:
Python3
# initializing list
test_list = [54, 67, 12, 45, 98, 76, 9]
# printing original list
print("The original list is : " + str(test_list))
# reversing and comparing for presence using in operator
res = [ele for ele in test_list if int(str(ele)[::-1]) not in test_list]
# printing result
print("List after palindromic removals ? : " + str(res))
Output:
The original list is : [54, 67, 12, 45, 98, 76, 9]
List after palindromic removals ? : [12, 98]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using filter(), str() and int()
In this, we perform the task of filtering each element using filter(). str() and int() is used for the same function as in the above method.
Example:
Python3
# initializing list
test_list = [54, 67, 12, 45, 98, 76, 9]
# printing original list
print("The original list is : " + str(test_list))
# reversing and comparing for presence using in operator
res = list(filter(lambda ele: int(str(ele)[::-1]) not in test_list, test_list))
# printing result
print("List after palindromic removals ? : " + str(res))
Output:
The original list is : [54, 67, 12, 45, 98, 76, 9]
List after palindromic removals ? : [12, 98]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The filter(), str() and int() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Similar Reads
Python Program to remove a specific digit from every element of the list Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
7 min read
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Python | Remove last element from each row in Matrix Sometimes, while working with Matrix data, we can have a stray element attached at rear end of each row of matrix. This can be undesired at times and wished to be removed. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination of th
6 min read
Python Program to find all Palindromic Bitlists in length In this article, we will learn to generate all Palindromic Bitlists of a given length using Backtracking in Python. Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. Whereas, Bitlists are list
6 min read
Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read