
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 Palindromic Elements from a List in Python
When it is required to remove palindromic elements from a list, list comprehension and the ‘not’ operator are used.
Example
Below is a demonstration of the same
my_list = [56, 78, 12, 32, 4,8, 9, 100, 11] print("The list is : ") print(my_list) my_result = [elem for elem in my_list if int(str(elem)[::-1]) not in my_list] print("The result is : " ) print(my_result)
Output
The list is : [56, 78, 12, 32, 4, 8, 9, 100, 11] The result is : [56, 78, 12, 32, 100]
Explanation
A list is defined and displayed on the console.
A list comprehension is used to iterate over the list, and convert the element to string first and then to an integer and reverse it.
It is checked to see that the element is not in the list.
This is assigned to a variable.
This is displayed as output on the console.
Advertisements