Find Palindromes and Anagrams in a List of Strings using Lambda

[ad_1]

This tutorial demonstrates how to find palindromes and anagrams in a list of strings using Python lambda. You’ll see fully working coding snippets and should be able to execute with Python 3.

We will use a Lambda expression inside the filter() function to find all the palindromes and anagrams in the string list. In Python, we also speak of lambda as the anonymous function, i.e., a function which doesn’t have a name.

Python Program – Find Palindromes and Anagrams

Let’s first write a Python program which takes a list of strings as input, find the palindromes, and print the result.

Use lambda to find palindromes in a list of strings

We’ll apply the anonymous function (Python lambda) to identify palindromes in the list of strings. First, let’s understand what’s a palindrome.

A palindrome is a word, digits, or a sequence of words that comes the same while reading backward.

See the full logic in the below coding snippet.

"""
Program:
 Python program to find palindromes in a list of strings
"""
def get_palindromes(input_string_list): 
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")

   # Find the list of palindromes in the strings    
   out_string_list = list(filter(lambda x: (x.lower() == "".join(reversed(x.lower()))), input_string_list)) 

   # Print the list of palindromes in the strings 
   print("*******************")
   print("out_string_list = ", out_string_list)
   print("*******************n")

def Driver(): 
   input_string_list = ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go']
   get_palindromes(input_string_list)

if __name__=="__main__": 
   Driver()          # call Driver() function

The result of the above Python code snippet is as follows:

*******************
input_string_list =  ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go']
*******************

*******************
out_string_list =  ['Radar', 'Madam', 'Noon', 'Refer', 'Php']
*******************

Sometimes, you might also need to convert a list to string, so have yourself go over it.

Use lambda to find anagrams in a list of strings

We’ll again apply the anonymous function to identify anagrams in the list of strings. First, let’s understand what’s an anagram.

An anagram is a word or phrase constituted by ordering the characters of a different word or phrase.

See the full logic in the below coding snippet.

"""
Program:
 Python program to find anagrams in a list of strings
"""
from collections import Counter

def get_anagrams(input_string_list, test_string): 
   
   print("*******************")
   print("input_string_list = ", input_string_list)
   print("*******************n")

   # Find the list of anagrams in the strings    
   out_string_list = list(filter(lambda x: (Counter(test_string) == Counter(x)), input_string_list)) 

   # Print the list of anagrams in the strings 
   print("*******************")
   print("out_string_list = ", out_string_list)
   print("*******************n")

def Driver(): 
   input_string_list = ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning']
   test_string = "ntoyPh"
   get_anagrams(input_string_list, test_string)

if __name__=="__main__": 
   Driver()          # call Driver() function

The result of the above Python code snippet is as follows:

*******************
input_string_list =  ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning']
*******************

*******************
out_string_list =  ['Python', 'yPtnoh']
*******************

To learn more, read our flagship Python tutorial for beginners and advanced learners.

[ad_2]

Leave a Comment