Find Palindromes and Anagrams in a List of Strings using Lambda – My programming school


This tutorial demonstrates how you can discover palindromes and anagrams in a listing of strings using Python lambda. You’ll see absolutely working coding snippets and ought to be capable to execute with Python 3.

We will use a Lambda expression contained in the filter() perform to seek out all of the palindromes and anagrams in the string listing. In Python, we additionally communicate of lambda as the nameless perform, i.e., a perform which doesn’t have a title.

Python Program – Find Palindromes and Anagrams

Let’s first write a Python program which takes a listing of strings as enter, discover the palindromes, and print the outcome.

Use lambda to seek out palindromes in a listing of strings

We’ll apply the nameless perform (Python lambda) to determine palindromes in the listing of strings. First, let’s perceive what’s a palindrome.

A palindrome is a phrase, digits, or a sequence of phrases that comes the identical while studying backward.

See the total logic in the beneath coding snippet.

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

   # Find the listing of palindromes in the strings    
   out_string_list = listing(filter(lambda x: (x.decrease() == "".be part of(reversed(x.decrease()))), input_string_list)) 

   # Print the listing 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()          # name Driver() perform

The outcome 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 may also must convert a list to string, so have your self go over it.

Use lambda to seek out anagrams in a listing of strings

We’ll once more apply the nameless perform to determine anagrams in the listing of strings. First, let’s perceive what’s an anagram.

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

See the total logic in the beneath coding snippet.

"""
Program:
 Python program to seek out anagrams in a listing 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 listing of anagrams in the strings    
   out_string_list = listing(filter(lambda x: (Counter(test_string) == Counter(x)), input_string_list)) 

   # Print the listing 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()          # name Driver() perform

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

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

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

To study extra, learn our flagship Python tutorial for freshmen and superior learners.

Python  Recommended Post

Source hyperlink

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.